如何保存BitmapImage的WinRT的 [英] How save BitmapImage WinRT

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

问题描述

我的BitmapImage:

I have BitmapImage:

BitmapImage image = new BitmapImage();
image.SetSource(memStream);



我想将图像保存到磁盘在未来看到的。我无法找到如何做到这一点的工作例子吗?第二个问题。我需要得到一个像素的颜色(如:彩色CL = image.GetColorPixel(X,Y)),我该怎么办呢?
谢谢!

I want to save the image to the disk to see in the future. I can not find a work example of how to do this? The second question. I need to get the color of a pixel (like: Color cl=image.GetColorPixel(X,Y)), how can I do that? Thank you!

推荐答案

下面是我发现前一段时间在网络代码。我不知道,但如果我rember权是从SDK的样本或WinRT的博客

Here's a code I found sometime ago in the web. I'm not sure, but if I rember right it was from the sdk samples or a winrt blog

这一切都归结到WritabelBitmap图像(像其他已经发布),创建一个解码器,并将它推到流

It all comes down to the WritabelBitmap Image ( like the other already posted), create a decoder and push it to the stream.

  /// <summary>
        /// 
        /// </summary>
        /// <param name="writeableBitmap"></param>
        /// <param name="outputFile"></param>
        /// <param name="encoderId"></param>
        /// <returns></returns>
        public static async Task SaveToFile(
            this WriteableBitmap writeableBitmap,
            IStorageFile outputFile,
            Guid encoderId)
        {
            try
            {
                Stream stream = writeableBitmap.PixelBuffer.AsStream();
                byte[] pixels = new byte[(uint)stream.Length];
                await stream.ReadAsync(pixels, 0, pixels.Length);

                using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
                    encoder.SetPixelData(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Premultiplied,
                        (uint)writeableBitmap.PixelWidth,
                        (uint)writeableBitmap.PixelHeight,
                        96,
                        96,
                        pixels);
                    await encoder.FlushAsync();

                    using (var outputStream = writeStream.GetOutputStreamAt(0))
                    {
                        await outputStream.FlushAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
            }
        }

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

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