×
Namespaces

Variants
Actions

如何在Windows Phone中压缩图片并保存

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon Multimedia.png
SignpostIcon WP7 70px.png
Article Metadata

代码示例
兼容于
文章
Hazel Xie 在 24 Oct 2012 创建
最后由 hamishwillee 在 29 Jun 2013 编辑

本文介绍了怎样降低图片的分辨率并将其保存到图片库。

介绍

将图片上传到服务器,但有时图片太大,为了节省流量需要对图片进行压缩后上传。我们可以利用PictureDecoder.DecodeJpeg(Stream, Int32, Int32)方法来返回一个 WriteableBitmap 类型的新图片。 下面介绍具体的实现步骤。

首先,我们使用以下代码添加按钮和图片控件到用户界面上。

  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image x:Name="img" Height="300" VerticalAlignment="Top" />
<Button Content="加载图片" Height="82" HorizontalAlignment="Left" Margin="150,0,0,219" Name="button1" VerticalAlignment="Bottom" Width="160" Click="button1_Click" />
<Button Content="保存图片" Height="72" HorizontalAlignment="Left" Margin="150,376,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
<Button Content="查看图片" Height="72" HorizontalAlignment="Left" Margin="150,438,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="105,535,0,0" Name="textBox1" Text="80" VerticalAlignment="Top" Width="114" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="225,535,0,0" Name="textBox2" Text="80" VerticalAlignment="Top" Width="116" />
<TextBlock Height="46" HorizontalAlignment="Left" Margin="12,555,0,0" Name="textBlock1" Text="设置目标分辨率" VerticalAlignment="Top" Width="90" />
</Grid>

下面,我们需要为项目添加必要的命名空间Microsoft.Xna.Framework,鼠标右击Reference,找到该命名空间点击添加。打开MainPage.xaml.cs,添加以下指令。

using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using Microsoft.Phone;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Xna.Framework.Media;

在按钮点击事件中我们将项目夹中的图片加载到用户界面上,注意记得将目标图片的Build Action设为Content。

private void button1_Click(object sender, RoutedEventArgs e)
{
//为JPEG文件创建一个流
StreamResourceInfo sri = null;
 
//将Tulips.jpg的Build Action设为Content
Uri jpegUri = new Uri("Images/Tulips.jpg", UriKind.Relative);
sri = Application.GetResourceStream(jpegUri);
 
//将该流解码
WriteableBitmap bitmap = PictureDecoder.DecodeJpeg(sri.Stream);
img.Source = bitmap;
}

接下来,获取用户界面上所输入的目标分辨率值并为按钮点击事件添加button2_Click中的代码,将转换过的图片保存到图片库中。

        int newPixelWidth;
int newPixelHeight;
 
public MainPage()
{
InitializeComponent();
 
//从页面读取输入值作为目标分辨率
newPixelWidth = int.Parse(textBox1.Text.ToString());
newPixelHeight = int.Parse(textBox2.Text.ToString());
 
textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
textBox2.TextChanged += new TextChangedEventHandler(textBox2_TextChanged);
}
 
void textBox2_TextChanged(object sender, TextChangedEventArgs e)
{
//textBox不为空
if (textBox2.Text != "" && int.Parse(textBox2.Text.ToString()) != 0)
{
newPixelHeight = int.Parse(textBox2.Text.ToString());
}
}
 
void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
if (textBox1.Text != "" && int.Parse(textBox1.Text.ToString()) != 0)
{
newPixelWidth = int.Parse(textBox1.Text.ToString());
}
}
 
private void button2_Click(object sender, RoutedEventArgs e)
{
String tempjpg = "tempimg";
 
//创建虚拟存储
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(tempjpg))
{
myStore.DeleteFile(tempjpg);
}
 
IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempjpg);
StreamResourceInfo sri = null;
sri = Application.GetResourceStream(new Uri("Images/Tulips.jpg", UriKind.Relative));
 
//创建WriteableBitmap对象
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
 
//WriteableBitmap对象编码,设置目标分辨率
wb.SaveJpeg(myFileStream, newPixelWidth, newPixelHeight, 0, 85);
myFileStream.Close();
 
myFileStream = myStore.OpenFile(tempjpg, FileMode.Open, FileAccess.Read);
 
//保存图片到图片库
MediaLibrary library = new MediaLibrary();
Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
 
myFileStream.Close();
}

最后,利用照片选择器PhotoChooserTask打开图片库,查看我们所保存的图片。

 //实例化选择器
PhotoChooserTask photoChooserTask = new PhotoChooserTask();
BitmapImage bimg;
 
private void button3_Click(object sender, RoutedEventArgs e)
{
//打开图片库
photoChooserTask.Show();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
 
}
 
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
bimg = new BitmapImage();
bimg.SetSource(e.ChosenPhoto);
//设置Image控件的数据源
img.Source = bimg;
}
}

项目调试

选择“调试|启动调试”菜单命令运行应用程序。下面是项目截图。

  • 1.JPG
  • 2.JPG
  • 3.JPG
  • 4 .JPG

下载

源代码:* File:ImageCompress.zip

This page was last modified on 29 June 2013, at 04:56.
98 page views in the last 30 days.