0

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
Dakaa
  • 199
  • 9
  • 3
    Because you have asked many questions here: stackoverflow is not for us to write your entire question. Please post concrete examples of where you are stuck. For example, posting a (possibly slow) example using setpixel would be good. Also, the question seems like premature optimization. – John Palmer Apr 06 '14 at 00:31
  • 1
    So you have a method using setpixel. Have you timed it to show how slow it is? (and whether you actually need it to go faster) – John Palmer Apr 06 '14 at 02:11
  • My own setpixel is just changing byte number in the 3d array, having problems turning back to an image, been 4 days lol. – Dakaa Apr 06 '14 at 02:45
  • 2
    One more thing - you have not voted yet. Lots of extremely knowledgeable people are taking time out of their day to try to help you. There are up and down arrows above/below the round circles beside the answers you are receiving - use them. – Ruben Bartelink Apr 06 '14 at 16:57
  • 1
    Okay, I went through all my questions and did that, thanks for reminding! – Dakaa Apr 06 '14 at 23:52

1 Answers1

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.

Community
  • 1
  • 1
Søren Debois
  • 5,598
  • 26
  • 48
  • Manipulate the values of array before unlocking the bits? and ditch the copy of 3d array? Been doing this for 3-4 days now, but I'll keep tyring. – Dakaa Apr 06 '14 at 12:05
  • 1
    No. In your previous question, a byte array called `values` is constructed. From that is then made an Array3D. If you stick to the `values` array, I'd imagine you can use the referenced constructor to get back to an image. (Once you have `values`, you can safely unlock.) – Søren Debois Apr 06 '14 at 12:24