如何:为 Windows Phone 编码 JPEG 并保存到图片库
本主题介绍如何将 WriteableBitmap 对象编码为 JPEG 流并将其添加到 Windows Phone 上的图片库中。执行以下任务:
-
添加相应的命名空间并为示例创建设计图面。
-
将 WriteableBitmap 对象编码为 JPEG 文件。
-
将此 JPEG 添加到 Windows Phone 上的图片库中。
重要说明: |
---|
对于当前版本的 Windows Phone,模拟器不支持在图片中心中查看照片。从该练习创建的照片只能在物理设备上查看。 |
若要开始,您需要创建一个新项目并添加所需的命名空间。
注意: |
---|
以下过程中的步骤用于 Visual Studio 2010 Express for Windows Phone。 当您使用用于 Visual Studio 2010 Professional 或 Visual Studio 2010 Ultimate 的插件时,您可能会看到菜单命令或窗口布局中的一些微小改变。 本主题基于 C# 开发;但也提供 Visual Basic 代码。 |
创建新项目并添加命名空间
-
在 Visual Studio 2010 Express for Windows Phone 中,通过选择“文件 | 新建项目”菜单命令创建一个新项目。
-
将显示“新建项目”窗口。展开“Visual C#”模板,然后选择“Silverlight for Windows Phone”模板。
-
选择“Windows Phone 应用程序”模板。用您选择的名称填写“名称”框。
-
单击“确定”。将显示“新建 Windows Phone 应用程序”窗口。
-
在“目标 Windows Phone 版本”菜单中,确保已选择 Windows Phone 7.1。
-
单击“确定”。将创建一个新的项目,并且“MainPage.xaml”将在 Visual Studio 设计器窗口中打开。
-
从“项目”菜单中,选择“添加引用”。在 .NET 选项卡中,选择 Microsoft.Xna.Framework 并单击“确定”。
-
打开主页的代码隐藏文件 MainPage.xaml.cs,在该页面的顶部添加以下指令。
创建设计图面的步骤
-
在 MainPage.xaml 中,将名为 LayoutRoot 的 Grid 替换为以下代码。
<!--LayoutRoot is the root grid where all page content is placed. --> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title. --> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="PHOTOS" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="encode" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - place additional content here. --> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Image Height="395" HorizontalAlignment="Left" Margin="6,6,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="444" Source="TestImage.jpg"/> <RadioButton Content="camera roll" Height="72" HorizontalAlignment="Center" Margin="96,407,90,0" Name="radioButtonCameraRoll" VerticalAlignment="Top" Width="270" GroupName="AlbumGroup"/> <RadioButton Content="saved pictures" Height="72" HorizontalAlignment="Right" Margin="0,453,90,0" Name="radioButtonSavedPictures" VerticalAlignment="Top" Width="270" GroupName="AlbumGroup"/> <Button Content="Save" Height="70" HorizontalAlignment="Center" Margin="6,519,0,0" Name="btnSave" VerticalAlignment="Top" Width="269" Click="btnSave_Click" /> </Grid> </Grid>
注意: 前面的代码创建一个图像控件(该控件占用该页面的顶部到中部的部分)和一个名为 btnSave 的按钮控件。可以在下图中看到这些控件的方向。
在本节中,将代码添加到 btnSave 单击事件处理程序以将 WriteableBitmap 对象编码为 JPEG 流,并将其添加到 Windows Phone 上的图片库中。
对 JPEG 文件进行编码并将其添加到库中的步骤
-
通过“解决方案资源管理器”将任何 JPEG 文件添加到您的项目。只需将照片拖动到要添加到的项目名称上。对于此示例,使用 TestImage.jpg 文件。确保您知道该文件在项目中的路径位置。在 MainPage.xaml 文件中,向名为 image1 的图像控件中添加以下属性和值。
Source=”TestImage.jpg”
注意: TestImage.jpg 只是一个示例。指定已添加到项目中的 JPEG 文件的名称,而不是 TestImage.jpg。
添加属性后,image1 控件应如下所示。
<Image Height="395" HorizontalAlignment="Left" Margin="6,6,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="468" Source="TestImage.jpg"/>
-
在 MainPage.xaml.cs 文件中,在 MainPage 构造函数中添加以下代码,放在 InitializeComponent 方法调用下面。这将选择本机拍照相册作为图像的默认存储位置。
-
双击 MainPage.xaml 中的 btnSave 按钮,以便为单击事件添加事件处理程序。将打开 MainPage.xaml.cs 文件。
-
在 MainPage.xaml.cs 文件中,用以下代码替换 btnSave_Click 方法。在该代码中,用项目的名称替换 [Application Name]。例如,如果您的项目名称为CameraEncode 并且图像命名为 TestImage.jpg,那么新 Uri 的第一个参数应为 "CameraEncode;component/TestImage.jpg"。
private void btnSave_Click(object sender, RoutedEventArgs e) { // Create a file name for the JPEG file in isolated storage. String tempJPEG = "TempJPEG"; // Create a virtual store and file stream. Check for duplicate tempJPEG files. var myStore = IsolatedStorageFile.GetUserStoreForApplication(); if (myStore.FileExists(tempJPEG)) { myStore.DeleteFile(tempJPEG); } IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG); // Create a stream out of the sample JPEG file. // For [Application Name] in the URI, use the project name that you entered // in the previous steps. Also, TestImage.jpg is an example; // you must enter your JPEG file name if it is different. StreamResourceInfo sri = null; Uri uri = new Uri("[Application Name];component/TestImage.jpg", UriKind.Relative); sri = Application.GetResourceStream(uri); // Create a new WriteableBitmap object and set it to the JPEG stream. BitmapImage bitmap = new BitmapImage(); bitmap.CreateOptions = BitmapCreateOptions.None; bitmap.SetSource(sri.Stream); WriteableBitmap wb = new WriteableBitmap(bitmap); // Encode the WriteableBitmap object to a JPEG stream. wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); myFileStream.Close(); // Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone. myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read); // Save the image to the camera roll or saved pictures album. MediaLibrary library = new MediaLibrary(); if (radioButtonCameraRoll.IsChecked == true) { // Save the image to the camera roll album. Picture pic = library.SavePictureToCameraRoll("SavedPicture.jpg", myFileStream); MessageBox.Show("Image saved to camera roll album"); } else { // Save the image to the saved pictures album. Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream); MessageBox.Show("Image saved to saved pictures album"); } myFileStream.Close(); }
-
通过选择“调试 | 启动调试”菜单命令运行应用程序。这将打开模拟器窗口并启动该应用程序。当您点按“保存”按钮时,您的 JPEG 文件将保存在“图片”中心的“本机拍照”或“保存的图片”相册中(取决于所选择的单选按钮)。
重要说明: 若要查看保存的图片,必须在物理设备上查看。在此版本中,模拟器不支持图片浏览。
下面的示例演示了完成的应用程序。