保存图像到媒体库在Windows Phone 7
问题描述:
我想从我的应用程序主页中的图像控件名为image1保存图像到手机的媒体库这里是我的代码,但在WriteableBitmap wr = image1;它给了我一个错误。保存图像到媒体库在Windows Phone 7
public void SaveImageTo(string fileName = "Gage.jpg")
{
fileName += ".jpg";
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(fileName))
{
myStore.DeleteFile(fileName);
}
IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName);
WriteableBitmap wr = image1; // 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); }
答
您正试图将Control
“图像1”分配给WriteableBitmap
对象,这就是为什么你有一个错误(它们是两种不同类型的)。
您应根据“image1”的来源设置不同来初始化WriteableBitmap
。
如果“图像1”引用了本地图像,可以初始化相应的WriteableBitmap
这样:
BitmapImage img = new BitmapImage(new Uri(@"images/yourimage.jpg", UriKind.Relative));
img.CreateOptions = BitmapCreateOptions.None;
img.ImageOpened += (s, e) =>
{
WriteableBitmap wr = new WriteableBitmap((BitmapImage)s);
};
如果你想渲染图像控制到一个WriteableBitmap的,你可以这样做:
WriteableBitmap wr = new WriteableBitmap(image1, null);
它工作的很棒! – user2015167 2013-02-12 15:35:26