2

我正在尝试将应用程序主页中名为 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); }
4

1 回答 1

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);
于 2013-02-11T16:43:27.657 回答