0

画像コントロールがあり、この画像を電話のストレージに保存したいと考えています。だから、私はイメージコントロールと下のボタンを持っています。ユーザーがボタンをクリックすると、画像が電話のストレージに保存されます。これどうやってするの ?

私はコードを見つけました:

 // Create a file name for the JPEG file in isolated storage.
        String tempJPEG = "TempJPEG";

        // Create a virtual store and file stream. Check for duplicate tempJPEG files.
        var myStore = IsolatedStorageFile.GetUserStoreForApplication();
        if (myStore.FileExists(tempJPEG))
        {
            myStore.DeleteFile(tempJPEG);
        }

        IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);


        // Create a stream out of the sample JPEG file.
        // For [Application Name] in the URI, use the project name that you entered 
        // in the previous steps. Also, TestImage.jpg is an example;
        // you must enter your JPEG file name if it is different.
        StreamResourceInfo sri = null;
        Uri uri = new Uri("[Application Name];component/TestImage.jpg", UriKind.Relative);
        sri = Application.GetResourceStream(uri);

        // Create a new WriteableBitmap object and set it to the JPEG stream.
        BitmapImage bitmap = new BitmapImage();
        bitmap.CreateOptions = BitmapCreateOptions.None;
        bitmap.SetSource(sri.Stream);
        WriteableBitmap wb = new WriteableBitmap(bitmap);

        // Encode the WriteableBitmap object to a JPEG stream.
        wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.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(tempJPEG, FileMode.Open, FileAccess.Read);

        // Save the image to the camera roll or saved pictures album.
        MediaLibrary library = new MediaLibrary();


        Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
        MessageBox.Show("Image saved to saved pictures album");


        myFileStream.Close();

しかし、ここに画像コントロールから画像を配置するにはどうすればよいですか?

4

1 に答える 1

3

イメージ コントロールから分離ストレージに保存する簡単な例。

Stream ImageStream = (MyImageControl.Source as BitmapImage).GetStream();

IsolatedStorageFile IsoStore = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream storageStream = IsoStore.CreateFile("TestImage.bmp"))                             
   ImageStream.CopyTo(storageStream);


public static Stream GetStream(this BitmapImage image)
{
    MemoryStream stream = new MemoryStream();              
    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(image));
    encoder.Save(stream);
    return stream;
}

GetStream拡張子は、この回答WPF Image から byte[] に取得されます。私はこのコードをテストしていませんが、正しい方向に進むはずです。

于 2013-11-05T16:22:40.153 に答える