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

时光怂恿深爱的人放手 提交于 2019-12-31 03:42:05

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/9423565/how-do-i-take-a-bitmap-image-and-save-as-a-jpeg-image-file-on-a-windows-phone-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!