问题
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