[英]Choosing image from windows phone media library and set as page background
[英]Saving an image to media library in Windows phone 7
我正在尝试将应用主页中名为image1的图像控件中的图像保存到手机的媒体库中,这是我的代码,但在WriteableBitmap中wr = image1; 这给了我一个错误。
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
“ image1”分配给WriteableBitmap
对象,这就是为什么您会出错(它们有2种不同的类型)的原因。
您应该根据如何设置“ image1”的来源来不同地初始化WriteableBitmap
。
如果“ image1”引用本地图像,则可以通过以下方式初始化相应的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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.