tags:

views:

535

answers:

5

Hi, i need to create a 24 bits bitmap (resolution 100x100 pixels) using a unique RGB color and save the generated image to the disk. currently i use the SetPixel function , but it is extremely slow.

Bitmap Bmp = new Bitmap(width, height);
//...
//...
Bmp.SetPixel(x,y,Color.FromARGB(redvalue, greenvalue, bluevalue));

There is a faster method than setpixel?

Thanks in advance.

+3  A: 

It depends on what you are trying to accomplish, but usually you would use GDI+ by getting a graphics object and then drawing to it:

Graphics g = Graphics.FromImage(bitmap);

Its actually a big subject, here are some beginner tutorials: GDI+ Tutorials

Here is a snippet from the tutorial on drawing a rectangle with a gradient fill.

Rectangle rect = new Rectangle(50, 30, 100, 100); 
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal); 
g.FillRectangle(lBrush, rect);
Ryan Cook
+1 for GDI+ suggestion.
Salvador
+1  A: 

You could use LockBits to speedup writing the pixels (pointer access instead of method call per pixel).

Gonzalo
A: 

I suggest checking out the GD Library.

I'm rather certain there is a c# library. http://www.boutell.com/gd/

David
A: 

You're spoilt for choice here :-)

An alternative to using GDI+ is to use WPF (see RenderTargetBitmap.Render.)

Also see this question.

Andrew Shepherd
+4  A: 

This should do what you need it to. It will fill the entire bitmap with the specified color.

using (Graphics gfx = Graphics.FromImage(Bmp))
using (SolidBrush brush = new SolidBrush(Color.FromArgb(redvalue, greenvalue, bluevalue)))
{
    gfx.FillRectangle(brush, 0, 0, width, height);
}
Jeromy Irvine
Thanks very much.
Salvador