问题描述
我有一个3D字节[,,]数组(i,x,y),里面有 i = B / G / R 和 x = x坐标和 y = y坐标。例如。 i = 2 x = 100 y = 200,像素的红色分量(100,200)或i = 0 x = 100 y = 200,像素的蓝色分量(100,200)是的我知道RGB的顺序相反。
I have an 3D byte[,,] array(i, x, y), inside it it has i = B/G/R and x = x coordinates and y = y coordinates. eg. i = 2 x = 100 y = 200, red component of pixel(100,200) or i = 0 x = 100 y = 200, blue component of pixel(100,200) Yes I know RGB is in reverse order.
现在,我不知道如何将这个字节[,,]转换为图像。我查看了。如果我使用SetPixel(),它会非常慢吗?如何避免使用SetPixel()?我正在尝试使用BitLock方法,避免使用GetPixel()或SetPixel(),我还有哪些替代方法可以做同样的事情?但速度更快。
Now, I'm not sure how to turn this byte[,,] into an image. I've looked at http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx. If I use SetPixel(), will it be really slow? How can I avoid using SetPixel()? I'm trying to go BitLock method, avoiding GetPixel() or SetPixel(), what other alternative methods do I have that does the same thing? But faster.
let greyscale (byteArray: byte[,,]) =
let file = new Bitmap("cat.jpg")
for j = 0 to file.Height - 1 do
for i = 0 to file.Width - 1 do
let p = Color.FromArgb(int byteArray.[2, i, j], int byteArray.[1, i, j], int byteArray.[0, i, j])
file.SetPixel(i, j, Color.FromArgb(greyValue p, greyValue p, greyValue p)) //greyValue returns average of RGB
file.Save("cat.jpg")
greyscale thisis3darray
推荐答案
你想要Bitmap构造函数(Int32,Int32,Int32,PixelFormat,IntPtr)
(文档 )。您必须将 byteArray
转换为匹配格式。
You want the Bitmap Constructor (Int32, Int32, Int32, PixelFormat, IntPtr)
(docs here). You'll have to convert your byteArray
into the matching format.
回顾,如果直接操作该问题的值
数组而不是构建中间 Array3D
,则可能更容易。
Looking back on your previous question, it's probably easier if you manipulate the values
array of that question directly instead of building an intermediate Array3D
.
这篇关于如何将字节数组转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!