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

查看:36
本文介绍了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.

我的问题是,我做错了什么.我想返回代表我应该拥有的 50,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

推荐答案

我有一个 WinRT 版本的 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天全站免登陆