C# 将图像保存到手机存储器
我有图像控制,我想将此图像保存到手机存储器中。 所以,我有图像控制和下面的按钮。当用户单击按钮时,图像应保存到手机存储器中。 我该怎么做 我找到了代码:C# 将图像保存到手机存储器,c#,windows-phone-7,windows-phone-8,windows-phone,windows-phone-7.1,C#,Windows Phone 7,Windows Phone 8,Windows Phone,Windows Phone 7.1,我有图像控制,我想将此图像保存到手机存储器中。 所以,我有图像控制和下面的按钮。当用户单击按钮时,图像应保存到手机存储器中。 我该怎么做 我找到了代码: // Create a file name for the JPEG file in isolated storage. String tempJPEG = "TempJPEG"; // Create a virtual store and file stream. Check for duplicate t
// 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();
但是我如何将图像从图像控制保存到这里呢?简单示例从图像控制保存到独立存储
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
扩展名取自此答案。我还没有测试过这段代码,但它应该会让您走上正轨。简单的示例从图像控制保存到独立存储
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
扩展名取自此答案。我还没有测试过这段代码,但它应该会让您走上正轨。简单的示例从图像控制保存到独立存储
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
扩展名取自此答案。我还没有测试过这段代码,但它应该会让您走上正轨。简单的示例从图像控制保存到独立存储
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
扩展名取自此答案。我还没有测试过这段代码,但它应该会让您走上正确的轨道。快速使用搜索功能将为您的问题提供多个答案。如果是这样,为什么不发布链接?你可以在Soboooom上的大多数问题上争论这个问题@MaximGershkovichA快速使用搜索功能将为您的问题提供多个答案。如果是这样,为什么不发布链接?你可以在Soboooom上的大多数问题上争论这个问题@MaximGershkovichA快速使用搜索功能将为您的问题提供多个答案。如果是这样,为什么不发布链接?你可以在Soboooom上的大多数问题上争论这个问题@MaximGershkovichA快速使用搜索功能将为您的问题提供多个答案。如果是这样,为什么不发布链接?你可以在Soboooom上的大多数问题上争论这个问题@Maximgershkovich对上述答案的唯一补充是,如果您害怕使用隔离存储。试试我写的免费DLL。它名为EZ_Iso.DLL,可免费使用并在下载,只需调用一个方法,即可将任何对象保存/检索到独立存储。完全消除了对上述所有复杂问题的需要以上答案的唯一补充是,如果您害怕使用隔离存储。试试我写的免费DLL。它名为EZ_Iso.DLL,可免费使用并在下载,只需调用一个方法,即可将任何对象保存/检索到独立存储。完全消除了对上述所有复杂问题的需要以上答案的唯一补充是,如果您害怕使用隔离存储。试试我写的免费DLL。它名为EZ_Iso.DLL,可免费使用并在下载,只需调用一个方法,即可将任何对象保存/检索到独立存储。完全消除了对上述所有复杂问题的需要以上答案的唯一补充是,如果您害怕使用隔离存储。试试我写的免费DLL。它名为EZ_Iso.DLL,可免费使用并在下载,只需调用一个方法,即可将任何对象保存/检索到独立存储。完全消除了上述所有复杂性的需要