Windows phone 7 保存可写位图
“mai”是包含图像、文本和小图像的网格名称。我正在关注一篇博客文章,内容是关于通过将图像制作成可写的位图(带有UIelment)来添加到图像中 当我在emulator上以调试模式运行时,会收到一条意外错误消息。我还将此应用程序部署到我的手机上(并断开了与计算机的连接),但收到了相同的错误 基本上,我正试图保存一张从相机胶卷上截取的图像,上面覆盖着一些文字。它喜欢将这个“新”图像保存到相机卷中 更新: 我也这样做了,结果也是一样的:Windows phone 7 保存可写位图,windows-phone-7,windows-phone-7.1,Windows Phone 7,Windows Phone 7.1,“mai”是包含图像、文本和小图像的网格名称。我正在关注一篇博客文章,内容是关于通过将图像制作成可写的位图(带有UIelment)来添加到图像中 当我在emulator上以调试模式运行时,会收到一条意外错误消息。我还将此应用程序部署到我的手机上(并断开了与计算机的连接),但收到了相同的错误 基本上,我正试图保存一张从相机胶卷上截取的图像,上面覆盖着一些文字。它喜欢将这个“新”图像保存到相机卷中 更新: 我也这样做了,结果也是一样的: WriteableBitmap wbm2 =
WriteableBitmap wbm2 = new WriteableBitmap(mai, null);
string tempjpeg = "tempmedicalertinfo";
// 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);
wbm2.SaveJpeg(myfilestream, 500, 500, 0, 100);
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();
// save the image to the saved pictures album.
try
{
Picture pic = library.SavePictureToCameraRoll("mai.jpg", myfilestream);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
myfilestream.Close();
更新:
错误的堆栈跟踪:
at Microsoft.Xna.Framework.Helpers.ThrowExceptionFromErrorCode(ErrorCodes error)
at Microsoft.Xna.Framework.Media.MediaLibrary.SavePicture(String name, Stream source)
at PB.MASetup.saveImage_Click(Object sender, EventArgs e)
at Microsoft.Phone.Shell.ApplicationBarItemContainer.FireEventHandler(EventHandler handler, Object sender, EventArgs args)
at Microsoft.Phone.Shell.ApplicationBarIconButton.ClickEvent()
at Microsoft.Phone.Shell.ApplicationBarIconButtonContainer.ClickEvent()
at Microsoft.Phone.Shell.ApplicationBar.OnCommand(UInt32 idCommand)
at Microsoft.Phone.Shell.Interop.NativeCallbackInteropWrapper.OnCommand(UInt32 idCommand)
问题在于流是定位字节数据的。因此,在将流传递到媒体库之前,必须将其返回到开始处。那会解决你的问题。下面是一个例子:(顺便说一句,对每个IDisposable对象使用using结构是一个很好的实践)
在我绞尽脑汁之后,我发现我的问题是WMAppManifest.xml中缺少功能
<Capability Name="ID_CAP_MEDIALIB" />
错误信息非常模糊,我不得不浪费大量时间来解决这个问题。您在哪种情况下称之为此?
wbm.PixelWidth
和wbm.PixelHeight是否有正确的值?我刚检查过,宽度值=456,高度值=535-它似乎获取了网格的大小:maiI将您的代码添加到Loaded
事件和所有事件中works@Ku6opr-你能给我举个例子吗?加载的事件是什么?我的代码在一个按钮点击事件中:private void saveImage_click(object sender,EventArgs e){…我可以用按钮和设备上的应用程序按钮和模拟器。该死!在我的电脑上敲了两天后,我才知道这一点。你在哪里???谢谢:-)
using (MemoryStream stream = new MemoryStream())
{
WriteableBitmap bitmap = new WriteableBitmap(LayoutRoot, null);
bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
using (MediaLibrary mediaLibrary = new MediaLibrary())
mediaLibrary.SavePicture("Picture.jpg", stream);
}
MessageBox.Show("Picture Saved...");
<Capability Name="ID_CAP_MEDIALIB" />