保存可写位图 [英] Saving a WriteableBitmap

查看:22
本文介绍了保存可写位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mai"是包含图像、文本和小图像的网格名称.我正在关注一篇关于能够通过将其设置为 WriteableBitmap(带有 UIelment)来添加到您的图像的博客文章.

"mai" is the grid name that contains the image, text and small image. I was following a blog post about being able add to your image by making it a WriteableBitmap (with a UIelment).

    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());
    }

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

When I run this in debug mode on the emulator I get an Unexpected Error message. I've also deployed this app to my phone (and disconnected it from the computer) and received the same error.

基本上,我试图保存从相机胶卷中选取的裁剪图像,并在其上覆盖一些文本.它喜欢将这个新"图像保存到相机胶卷中.

Basically I'm trying to save a cropped image picked from the Camera Roll with some text overlayed on top of it. It like to save this "new" image into the Camera Roll.

更新:

我也做了同样的结果:

        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 结构是一个好习惯)

The problem is that the streams are positioned byte data. So before you can pass your stream to the media library you must seek it back to the begin. That will solve your problem. Here is an example: (btw it's a good practice to use the using structure for every IDisposable object)

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...");

这篇关于保存可写位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆