WinRT将图像加载到字节数组中 [英] WinRT Loading an Image into a Byte array

查看:74
本文介绍了WinRT将图像加载到字节数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何与某些WinRT(Metro)对象进行图像交互.基本上,我正在努力创建一种易于使用的"GetPixel"和"SetPixel"方法(类似于System.Drawing.Bitmap中的功能,该方法在Metro应用程序中无法使用).

I'm trying to learn how to interact with the some of the WinRT (Metro) objects for images. Basically, I'm working towards creating a "GetPixel" and "SetPixel" method that's easy to use (similiar to what's in System.Drawing.Bitmap that can't be used in Metro apps).

为了进行测试,我创建了一个位图"类.它基于我从Metro文件选择器控件中获得的IRandomAccessStream进行加载.我能够将像素数据加载到Byte数组中,然后从中创建一个BitmapImage,当我将其扔到Image控件中时,它可以在XAML表单上正确呈现.

For testing, I created a "Bitmap" class. It loads based off of a IRandomAccessStream that I get from the Metro file picker control. I'm able to load the pixel data into a Byte array and then create a BitmapImage from it that renders correctly on a XAML form when I toss it into an Image control.

这是我的问题,我可以查看字节数组并查看各个ARGB值,但是像素远少于应有的像素数.例如,我正在加载的图像为254x197像素(254 * 197 * 4 = 200,152).当我使用以下代码加载Byte数组时,它只有16,382个字节(也不会平均除以4).我想这是压缩的吗?..我不知道.

Here's my issue, I can look at the byte array and see the individual ARGB values, but, it has far less pixels than it should. For example, the image I'm loading is 254x197 pixels (254*197*4=200,152). When I load my Byte array though with the below code, it's only got 16,382 bytes (which also doesn't equally divide by 4). I assume, this is compressed?.. I dunno.

我的问题是,我在做什么错..我想返回代表我应该拥有的500,038像素的200,152字节,以便可以创建GetPixel(x,y)和SetPixel(x,y,Color)方法.

My question is, what am I doing wrong.. I would like to return the 200,152 bytes representing the 50,038 pixels that I should have so I can create GetPixel(x,y) and a SetPixel(x,y,Color) methods.

Public Class Bitmap

    Public Sub New()

    End Sub

    Public Async Function Load(s As Windows.Storage.Streams.IRandomAccessStream) As Task
        Dim dr As New DataReader(s.GetInputStreamAt(0))
        Await dr.LoadAsync(s.Size)
        Me.Pixels = New Byte(s.Size - 1) {}
        dr.ReadBytes(Me.Pixels)

        ' We're going to get the height and the width of the image this way. ;)
        Dim bi As New BitmapImage
        Dim stream As New MemoryStream(Me.Pixels)
        bi.SetSource(New RandomStream(stream))
        Me.Width = bi.PixelWidth
        Me.Height = bi.PixelHeight
    End Function

    Public Function ToBitmapImage() As BitmapImage
        Dim bi As New BitmapImage
        Dim stream As New MemoryStream(Me.Pixels)
        bi.SetSource(New RandomStream(stream))
        Return bi
    End Function

    Public Property Pixels As Byte()
    Public Property Width As Integer = 0
    Public Property Height As Integer = 0

End Class

推荐答案

我有 WriteableBitmapEx 项目此处,它可以满足您的需求.基本上,您需要将图像加载到WriteableBitmap中,然后通过调用PixelBuffer.AsStream()访问其像素缓冲区.然后,您需要将流搜索到x,y位置(y * bmp.PixelWidth + x)才能读取像素值.然后,将获得的4个字节转换为适合您的像素格式.

I have a WinRT version of the WriteableBitmapEx project here that does what you are looking for. Basically you need to load the image into a WriteableBitmap, then access its pixel buffer by calling PixelBuffer.AsStream(). Then you need to seek the stream to the x,y position (y * bmp.PixelWidth + x) to be able to read the pixel value. Then also convert the 4 bytes you get to the pixel format that works for you.

编辑*

WriteableBitmapEx现在本身就支持WinRT.

WriteableBitmapEx does natively support WinRT now.

这篇关于WinRT将图像加载到字节数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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