将图像保存到Windows Phone 7中的媒体库

时间:2013-02-11 15:53:33

标签: 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.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); }

1 个答案:

答案 0 :(得分:0)

您正尝试将Control“image1”分配给WriteableBitmap对象,这就是您遇到错误的原因(它们是2种不同的类型)。

您应该以不同的方式初始化WriteableBitmap,具体取决于“image1”的来源设置方式。

如果“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);
};

如果你想将渲染你的Image控件变成WriteableBitmap,你可以这样做:

WriteableBitmap wr = new WriteableBitmap(image1, null);