1

我无法将图像保存到 Windows Phone 媒体库中!这是我的代码片段:

public void SaveImageTo(string fileName) 
    {
        fileName += ".jpg";
        var myStore = IsolatedStorageFile.GetUserStoreForApplication();
        if (myStore.FileExists(fileName))
        {
            myStore.DeleteFile(fileName);
        }

        IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName);                       

        // 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();

        library.SavePicture(fileName, myFileStream);
    }

请给点建议!提前致谢 !

4

2 回答 2

1

尝试这个 ....

public void SaveImageTo(string fileName = "ShareByQR.jpg") 
    {
        fileName += ".jpg";
        var myStore = IsolatedStorageFile.GetUserStoreForApplication();
        if (myStore.FileExists(fileName))
        {
            myStore.DeleteFile(fileName);
        }

        IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName);
        WritableBitmap wr = imageControl; // 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);
    }

希望它有效!

于 2012-07-02T05:09:19.127 回答
0

我将 Shishir 的代码段更改为读取外部 url。这是我的方法:

    public void SaveImageTo(string fileName = "SO.jpg")
    {
        string source = "http://i.stack.imgur.com/PIFN0.jpg";
        Uri url;
        if (Uri.TryCreate(source, UriKind.Absolute, out url))
        {
            WriteableBitmap wr;
            BitmapImage img = new BitmapImage(url);
            img.CreateOptions = BitmapCreateOptions.None;
            // When image is ready, show must go on.
            img.ImageOpened += (s, e) =>
            {
                wr = new WriteableBitmap((BitmapImage)s);
                //fileName += ".jpg"; // we dont need that
                var myStore = IsolatedStorageFile.GetUserStoreForApplication();
                if (myStore.FileExists(fileName))
                {
                    myStore.DeleteFile(fileName);
                }
                IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName);
                //WriteableBitmap wr = img; // image source already given
                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);
            };
        }
    }

不要忘记在 WMAppManifest.xml 中添加 ID_CAP_MEDIALIB_PHOTO 。我希望这也能有所帮助。

于 2013-09-17T14:25:24.437 回答