fast way to Draw RGB/Grayscale Bitmap from a Double Matrix

P

ProtossLee

Hi,
I am currently working on a project for image processing.
a double matrix m1(1300X1000) need to be converted into bitmap and
displayed on screen.
so far I've made the following code:

For i = 1 To 1300
For j = 1 To 1000
Sgl = Matrix(i, j)
color = CInt(255 * (Sgl - SglMin) / (SglMax - SglMin))
bmp.SetPixel(i, j,
System.Drawing.Color.FromArgb(color, color, color))
Next j
Next i

Basically, I rescale each element of the matrix into [0,255] and draw
the pixel of the corresponding bitmap.

However, this takes too much time (more than 1sec).

Is there any better way to do this?
 
T

Tom Shelton

Hi,
I am currently working on a project for image processing.
a double matrix m1(1300X1000) need to be converted into bitmap and
displayed on screen.
so far I've made the following code:

For i = 1 To 1300
For j = 1 To 1000
Sgl = Matrix(i, j)
color = CInt(255 * (Sgl - SglMin) / (SglMax - SglMin))
bmp.SetPixel(i, j,
System.Drawing.Color.FromArgb(color, color, color))
Next j
Next i

Basically, I rescale each element of the matrix into [0,255] and draw
the pixel of the corresponding bitmap.

However, this takes too much time (more than 1sec).

Is there any better way to do this?

I don't know about better, but you most definately can get a speed
improvement by converting this block of code to C# using and unsafe code
block. Then you can access each pixel in memory directly, instead of
caling the SetPixel method - which is very slow.
 
M

Michael Phillips, Jr.

Instead of using SetPixel, roll your own equivalent and use a byte
array(1300X1000) that you store the color.ToArgb() and use that byte array
to create your bitmap.

Use the Bitmap(width, height, stride, format, scan0) constructor.

The benefits are no unsafe code and you only use marshaling once to copy the
entire byte array to the scan0, IntPtr.
 
M

Michael Phillips, Jr.

You are better off of using a byte array and writing the individual blue,
green , and red color bytes. The allocated array will be 1300x1000x3 bytes
for 24bpp or 1300x1000x4 for a 32bpp image.

At the end you need to marshal your array to the Scan0 IntPtr that is used
to create the image.


Michael Phillips said:
Instead of using SetPixel, roll your own equivalent and use a byte
array(1300X1000) that you store the color.ToArgb() and use that byte array
to create your bitmap.

Use the Bitmap(width, height, stride, format, scan0) constructor.

The benefits are no unsafe code and you only use marshaling once to copy
the entire byte array to the scan0, IntPtr.

Hi,
I am currently working on a project for image processing.
a double matrix m1(1300X1000) need to be converted into bitmap and
displayed on screen.
so far I've made the following code:

For i = 1 To 1300
For j = 1 To 1000
Sgl = Matrix(i, j)
color = CInt(255 * (Sgl - SglMin) / (SglMax - SglMin))
bmp.SetPixel(i, j,
System.Drawing.Color.FromArgb(color, color, color))
Next j
Next i

Basically, I rescale each element of the matrix into [0,255] and draw
the pixel of the corresponding bitmap.

However, this takes too much time (more than 1sec).

Is there any better way to do this?
 
T

Tom Shelton

Instead of using SetPixel, roll your own equivalent and use a byte
array(1300X1000) that you store the color.ToArgb() and use that byte array
to create your bitmap.

Use the Bitmap(width, height, stride, format, scan0) constructor.

The benefits are no unsafe code and you only use marshaling once to copy the
entire byte array to the scan0, IntPtr.

Interesting idea... Hadn't thought of that one. I'll have to try it
out, just to see what the speed difference would be on that.
 
Top