简体   繁体   中英

How do I take a bitmap image and save as a JPEG image file on a Windows Phone 7 device?

I am looking to create a function that takes a BitmapImage and saves it as a JPEG on the local Windows Phone 7 device in isolated storage:

static public void saveImageLocally(string barcode, BitmapImage anImage)
{
 // save anImage as a JPEG on the device here
}

How do I accomplish this? I'm assuming I used IsolatedStorageFile somehow?

Thanks.

EDIT:

Here is what I have found so far... can anyone confirm if this is the correct way to do this?

    static public void saveImageLocally(string barcode, BitmapImage anImage)
    {
        WriteableBitmap wb = new WriteableBitmap(anImage);

        using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (var fs = isf.CreateFile(barcode + ".jpg"))
            {
                wb.SaveJpeg(fs, wb.PixelWidth, wb.PixelHeight, 0, 100);
            }
        }
    }

    static public void deleteImageLocally(string barcode)
    {
        using (IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            MyStore.DeleteFile(barcode + ".jpg");
        }
    }

    static public BitmapImage getImageWithBarcode(string barcode)
    {
        BitmapImage bi = new BitmapImage();

        using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (var fs = isf.OpenFile(barcode + ".jpg", FileMode.Open))
            {
                bi.SetSource(fs);
            }
        }

        return bi;
    }

To save it:

var bmp = new WriteableBitmap(bitmapImage);
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = storage.CreateFile(@"MyFolder\file.jpg"))
        {
            bmp.SaveJpeg(stream, 200, 100, 0, 95);
            stream.Close();
        }
    }

Yes, the stuff you added in your edit is exactly what I have done before :) it works.

This is my code but you can take the neccesary points from there:

        var fileName = String.Format("{0:}.jpg", DateTime.Now.Ticks);
        WriteableBitmap bmpCurrentScreenImage = new WriteableBitmap(480, 552);
        bmpCurrentScreenImage.Render(yourCanvas, new MatrixTransform());
        bmpCurrentScreenImage.Invalidate();
        SaveToMediaLibrary(bmpCurrentScreenImage, fileName, 100);


    public void SaveToMediaLibrary(WriteableBitmap bitmap, string name, int quality)
    {
        using (var stream = new MemoryStream())
        {
            // Save the picture to the Windows Phone media library.
            bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, quality);
            stream.Seek(0, SeekOrigin.Begin);
            new MediaLibrary().SavePicture(name, stream);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM