Question

i am trying to save an image from an image control named image1 in my app mainpage to phone's media library here is my code, but in WriteableBitmap wr = image1; it gives me an error.

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); }
Was it helpful?

Solution

You are trying to assign the Control "image1" to a WriteableBitmap object, that's why you have an error (they are 2 different types).

You should initialize the WriteableBitmap differently, depending on how the source of "image1" was set.

If "image1" references a local image, you can initialize the corresponding WriteableBitmap that way:

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);
};

If you want to render your Image control into a WriteableBitmap, you can do that:

WriteableBitmap wr = new WriteableBitmap(image1, null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top