468,507 Members | 1,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,507 developers. It's quick & easy.

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

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?
Mar 31 '08 #1
6 3574
On 2008-03-31, Pr********@googlemail.com <Pr********@googlemail.comwrote:
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.

--
Tom Shelton
Mar 31 '08 #2
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.

<Pr********@googlemail.comwrote in message
news:5d**********************************@c26g2000 prf.googlegroups.com...
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?


Mar 31 '08 #3
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, Jr." <mp*********@nospam.jun0.c0mwrote in message
news:uJ**************@TK2MSFTNGP03.phx.gbl...
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.

<Pr********@googlemail.comwrote in message
news:5d**********************************@c26g2000 prf.googlegroups.com...
>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?



Mar 31 '08 #4
On 2008-03-31, Michael Phillips, Jr. <mp*********@nospam.jun0.c0mwrote:
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.

--
Tom Shelton
Apr 1 '08 #5
On Mar 31, 9:41 pm, Tom Shelton
<tom_shel...@YOUKNOWTHEDRILLcomcast.netwrote:
On 2008-03-31, Michael Phillips, Jr. <mphillip...@nospam.jun0.c0mwrote:
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.

--
Tom Shelton
I think there might be an article on Bob Powell's site that talks
about this (I'm not sure though):

http://www.bobpowell.net

Chris
Apr 1 '08 #6
On Apr 1, 5:17 pm, Chris Dunaway <dunaw...@gmail.comwrote:
On Mar 31, 9:41 pm, Tom Shelton

<tom_shel...@YOUKNOWTHEDRILLcomcast.netwrote:
On 2008-03-31, Michael Phillips, Jr. <mphillip...@nospam.jun0.c0mwrote:
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.
--
Tom Shelton

I think there might be an article on Bob Powell's site that talks
about this (I'm not sure though):

http://www.bobpowell.net

Chris
Yes it does have some very valuable tips there.
And I thank everybody who replied this thread.
Apr 1 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by RicercatoreSbadato | last post: by
2 posts views Thread by Harry Simpson | last post: by
2 posts views Thread by predseda | last post: by
8 posts views Thread by platinumhimani | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.