C# 将图像保存到Windows phone 7中的媒体库

C# 将图像保存到Windows phone 7中的媒体库,c#,image,windows-phone-7,media-library,C#,Image,Windows Phone 7,Media Library,我正在尝试将我的应用程序主页中名为image1的图像控件中的图像保存到手机的媒体库。这是我的代码,但为WriteableBitmap wr=image1;这给了我一个错误 public void SaveImageTo(string fileName = "Gage.jpg") { fileName += ".jpg"; var myStore = IsolatedStorageFile.GetUserStoreForApplication(); if (myStore.

我正在尝试将我的应用程序主页中名为image1的图像控件中的图像保存到手机的媒体库。这是我的代码,但为WriteableBitmap wr=image1;这给了我一个错误

public void SaveImageTo(string fileName = "Gage.jpg")
{
    fileName += ".jpg";
    var myStore = IsolatedStorageFile.GetUserStoreForApplication();
    if (myStore.FileExists(fileName))
    {
        myStore.DeleteFile(fileName);
    }

    IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName);
    WriteableBitmap wr = image1; // give the image source
    wr.SaveJpeg(myFileStream, wr.PixelWidth, wr.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(fileName, FileMode.Open, FileAccess.Read);
    MediaLibrary library = new MediaLibrary();
    //byte[] buffer = ToByteArray(qrImage);
    library.SavePicture(fileName, myFileStream); }

您试图将
控件
的“image1”分配给
可写位图
对象,这就是出现错误的原因(它们是两种不同的类型)

根据“image1”源的设置方式,您应该以不同的方式初始化
WriteableBitmap

如果“image1”引用本地映像,则可以通过以下方式初始化相应的
WriteableBitmap

BitmapImage img = new BitmapImage(new Uri(@"images/yourimage.jpg", UriKind.Relative));
img.CreateOptions = BitmapCreateOptions.None;
img.ImageOpened += (s, e) =>
{
    WriteableBitmap wr = new WriteableBitmap((BitmapImage)s);
};
如果要将图像控件渲染到可写位图中,可以执行以下操作:

WriteableBitmap wr = new WriteableBitmap(image1, null);