Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

c# - store image into isolated storage in windows phone 7

Basically I am using Visual Studio/Expression Blend to do my app. Its works as in the user can select the picture that he/she wants to edit and after editing the user just had to click the save button and the edited image will be save in isolated storage but I just could not command the save button to save the image into the isolated storage so hope someone will help me with it with some sample codes thanks a lot in advance.

I tried with the code below but when I press the save button there's a null reference error. My thinking is that when you press save, the app does not know which image to save into the isolated storage and not really sure my thinking is right. Can anyone please help me with this. Thanks a lot.

private void btnSave_Click(object sender, RoutedEventArgs e)
{
    String tempJPEG = "TempJPEG";

    var myStore = IsolatedStorageFile.GetUserStoreForApplication();
    if (myStore.FileExists(tempJPEG))
    {
        myStore.DeleteFile(tempJPEG);
    }

    IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);

    Uri uri = new Uri("TestImage.jpg", UriKind.Relative);
    StreamResourceInfo sri = Application.GetResourceStream(uri);

    BitmapImage bitmap = new BitmapImage();
    bitmap.CreateOptions = BitmapCreateOptions.None; 
    bitmap.SetSource(sri.Stream);
    WriteableBitmap wb = new WriteableBitmap(bitmap);

    Extensions.SaveJpeg(wb, myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
    myFileStream.Close();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the working version of the code

private void saveButtonClick(object sender, RoutedEventArgs e)
{
    try
    {
        using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (isf.FileExists("myImage.jpg"))
                isf.DeleteFile("myImage.jpg");
            using (var isfs = isf.CreateFile("myImage.jpg"))
            {
                var bmp = new WriteableBitmap(myImageElement,
                                myImageElement.RenderTransform);
                bmp.SaveJpeg(isfs, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
            }
        }
    }
    catch (Exception exc)
    {
        MessageBox.Show(exc.Message);
    }
}

Here myImageElement is the Image Element in which you display the image.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...