2

我在图像控件中有一个图像,如下所示:

<Image x:name="myImg" Source="Images/MyImg.png" />

如何将此图像保存到图像库中,以便我可以通过转到图库文件夹来查看。

我尝试了不同的代码,但无法保存。请帮助我。

编辑:

我在列表框中控制图像。我正在将列表框与来自 Web 服务的 IList 绑定。

因此,在绑定图像后,如果用户想要保存图像,他可以保存他想要保存的特定内容。

那么我怎样才能保存那个特定的图像。

提前致谢。

4

1 回答 1

1

有一篇出色的 MSDN 文章描述了这种情况: 如何:为 Windows Phone 编码 JPEG 并保存到图片库

windows phone save image to media library顺便说一句,它也是在GoogleBing上搜索时的第一个搜索结果链接。

您是否尝试过遵循本指南,如果是,您有什么问题?

保存它的基本代码:

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();
}
于 2012-06-04T08:08:55.920 回答