Saving image to media library WP7

后端 未结 2 1465
甜味超标
甜味超标 2021-01-16 02:58

I cant save images into windows phone media library ! Here is my code snippet :

public void SaveImageTo(string fileName) 
    {
        fileName += \".jpg\";         


        
相关标签:
2条回答
  • 2021-01-16 03:28

    I changed Shishir's snippet to read extern url. This is my method :

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

    Dont forget to add ID_CAP_MEDIALIB_PHOTO in WMAppManifest.xml . I hope this can help too.

    0 讨论(0)
  • 2021-01-16 03:53

    try this ....

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

    hope it works!

    0 讨论(0)
提交回复
热议问题