0

I have a 256x256 matrix and each element of the matrix: myMatrix[i,j] // represents a pixel How can I store my matrix in a picture Box in C#?

noob234
  • 199
  • 2
  • 12
  • You can create a Bitmap bmp = new Bitmap(256,256) and do a double loop if SetPixel. Then pbox.Image = bmp. – TaW Dec 04 '20 at 14:46

1 Answers1

2

The simplest method is to create a bitmap of equal size and use SetPixel for each x,y value.

You will need some kind of transform between the values in the matrix and the color values in the image. If your values is in [0,255] you can just clamp the values. Otherwise the simplest method would be to use linear interpolation between min and max:

var i = (byte)((matrixValue - matrixMinValue) * 255 / (matrixMinValue - matrixMinValue)) 
var color  = Color.FromArgb(i,i,i);
myBitmap.SetPixel(x, y, color)

Set the bitmap to the pictureBox.Image property and you should be good to go.

SetPixel should be adequate for a small image that is set infrequently. If you need higher performance you can look at using pointers to copy data.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • @noob234 you probably have a column-major order matrix. Images are usually row-major. See https://en.wikipedia.org/wiki/Row-_and_column-major_order – JonasH Dec 06 '20 at 13:36