转换的ImageSource在Metro Windows中WriteableBitmap的8 [英] Convert ImageSource to WriteableBitmap in Metro Windows 8

查看:177
本文介绍了转换的ImageSource在Metro Windows中WriteableBitmap的8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将一个 ImageSource 对象(在我的情况下,图像元素的源属性)转换为 WriteableBitmap WriteableBitmap(BitmapSource)构造函数在Windows 8 Metro中不起作用,因此我不能这样做。



我试图将 ImageSource 对象加载到流中,然后使用 WriteableBitmap .SetSource(stream),但我不知道如何加载流中的图像。

  InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream 
BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId,ras);

byte [] pixelArray = new byte [(uint)photoBox.Height *(uint)photoBox.Width * 4];

enc.SetPixelData(BitmapPixelFormat.Rgba8,BitmapAlphaMode.Straight,
(uint)photoBox.Width,(uint)photoBox.Height,96,96,pixelArray);

我还添加了我在网上找到的帮助方法:

  public Byte [] BufferFromImage(BitmapImage imageSource)
{
Stream stream = imageSource.StreamSource;
Byte [] buffer = null;
if(stream!= null&& stream.Length> 0)
{
using(BinaryReader br = new BinaryReader(stream))
{
buffer = br.ReadBytes((Int32)stream.Length);
}
}

返回缓冲区;
}

问题是 BitmapImage 不再具有 StreamSource 方法。

解决方案

我在MSDN论坛上得到了答案。


没有办法从ImageSource提取图像数据。您将需要跟踪信息的原始来源,并从原始来源重新创建WriteableBitmap中的图像。如果你知道你需要这样做,你最好只是使用WriteableBitmap作为你的ImageSource开始。


来源


I was wondering how I could go about converting an ImageSource object (in my case the source property of an image element) to a WriteableBitmap. The WriteableBitmap(BitmapSource) constructor doesn't work in Windows 8 Metro, so I can't do that.

I've tried to load the ImageSource object into a stream and then use WriteableBitmap.SetSource(stream), but I can't figure out how do load the image in a stream either.

InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras);

byte[] pixelArray = new byte[(uint)photoBox.Height * (uint)photoBox.Width * 4];

enc.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight,
                 (uint)photoBox.Width, (uint)photoBox.Height, 96, 96, pixelArray);

I've also added this helper method I found online:

public Byte[] BufferFromImage(BitmapImage imageSource)
{
    Stream stream = imageSource.StreamSource;
    Byte[] buffer = null;
    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }

    return buffer;
}

The problem is that BitmapImage no longer has the StreamSource method.

解决方案

I got an answer on the MSDN forums.

There is no way to extract the image data from an ImageSource. You will need to keep track of the original source of the information and recreate the image in the WriteableBitmap from the original source. If you know you are going to need to do this you may be better off just using a WriteableBitmap as your ImageSource to begin with.

Source

这篇关于转换的ImageSource在Metro Windows中WriteableBitmap的8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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