将图像保存到媒体库WP7

时间:2012-07-02 05:04:46

标签: c# .net silverlight windows-phone-7

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

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

请给出一些建议! 提前谢谢!

2 个答案:

答案 0 :(得分: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);
    }

希望它有效!

答案 1 :(得分:0)

我改变了Shishir的片段来阅读extern 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。 我希望这也有帮助。