How to turn byte array into an image?

…衆ロ難τιáo~ 提交于 2020-01-06 08:20:08

问题


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.

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

回答1:


You want the Bitmap Constructor (Int32, Int32, Int32, PixelFormat, IntPtr) (docs here). You'll have to convert your byteArray into the matching format.

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.



来源:https://stackoverflow.com/questions/22888473/how-to-turn-byte-array-into-an-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!