首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存WriteableBitmap

保存WriteableBitmap
EN

Stack Overflow用户
提问于 2012-03-09 13:40:01
回答 2查看 4.8K关注 0票数 2

"mai“是包含图像、文本和小图像的网格名称。我在博客上写了一篇文章,内容是通过WriteableBitmap (用UIelment)添加到你的图像中。

代码语言:javascript
复制
    try
    {
        WriteableBitmap wbm = new WriteableBitmap(mai, null);

        MediaLibrary ml = new MediaLibrary();
        Stream stream = new MemoryStream();

        wbm.SaveJpeg(stream, wbm.PixelWidth, wbm.PixelHeight, 0, 100);
        ml.SavePicture("mai.jpg", stream);
        MessageBox.Show("Picture Saved...");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }

当我在模拟器上以调试模式运行这个程序时,我会收到一条意外错误消息。我还将这个应用程序部署到我的手机上(并将其与计算机断开连接),并收到了同样的错误。

基本上,我试着保存一个从相机辊上选择的剪裁图像,上面覆盖了一些文本。它喜欢将这个“新”图像保存到相机滚筒中。

更新:

我也是这样做的,结果是一样的:

代码语言:javascript
复制
        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();

更新:

错误的堆栈跟踪:

代码语言:javascript
复制
   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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-09 15:31:05

问题是流被定位为字节数据。因此,在将您的流传递到媒体库之前,您必须从开始时开始寻找它。这会解决你的问题。下面是一个示例:(顺便说一句,对每个IDisposable对象使用using结构是一个很好的实践)

代码语言:javascript
复制
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...");
票数 10
EN

Stack Overflow用户

发布于 2012-04-22 08:25:32

在经历了很多次的失败之后,我发现我的问题是缺少了WMAppManifest.xml的功能

代码语言:javascript
复制
  <Capability Name="ID_CAP_MEDIALIB" />

错误信息是如此模糊,以至于我不得不浪费大量的时间来解决这个问题。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9634970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档