53

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

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

Is there a faster method than SetPixel?

8 Answers 8

83

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

Bitmap Bmp = new Bitmap(width, height);
using (Graphics gfx = Graphics.FromImage(Bmp))
using (SolidBrush brush = new SolidBrush(Color.FromArgb(redvalue, greenvalue, bluevalue)))
{
    gfx.FillRectangle(brush, 0, 0, width, height);
}
6
  • This shows how to make a bitmap from another bitmap. Mar 31, 2016 at 23:36
  • 2
    @SanuelJackson So you downvoted this answer because it answers the OPs actual question rather than the title of his question? Apr 1, 2016 at 13:39
  • 1
    If you add Bitmap Bmp = new Bitmap(width, height);, then it is correct. I can see in the question it also has a code example on what is currently being done to create something from scratch. Was more or less tired of seeing the Graphics.FromImage() in all the 'from scratch' answers. Ideally would like to see a fresh Bitmap from Graphics from scratch. Apr 1, 2016 at 20:40
  • 1
    @SanuelJackson Fair enough, I suppose, though nobody asked how to create one from scratch - the OP already knew how to do that, and his real question was how to fill a bitmap with solid color. Perhaps you could focus your attention on improving the question title with a suggested edit. Or, if you know how to fix the answer, just go ahead and make an edit to it. Apr 3, 2016 at 15:46
  • Switched my vote. Thank you for updating this. I try not to edit posts that are old (shy when it comes to necro-editing). Definitely helpful for those searching for this concept as this post shows up fairly high in google search now :) Apr 3, 2016 at 16:17
48
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Green);
4
  • 1
    Could you provide us more details related with your answer. Thank you
    – Robert
    Feb 20, 2017 at 20:05
  • 7
    This is the best answer here. In my experience Clear is faster than calling FillRectangle when you're clearing the whole image. May 19, 2017 at 10:44
  • Graphics clear method clears the entire drawing surface and fills it with the specified background color. There is no need to manually fill it, this is an option that clear method has. look to the msdn explanation. Jun 27, 2017 at 4:25
  • 4
    If you read this, always use using guards around any operation using bitmaps or graphics, or the objects won't be disposed of correctly.
    – Mafii
    May 23, 2019 at 16:22
6

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); 
0
3

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

3

You're spoilt for choice here :-)

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

Also see this question.

1
  • This should be the answer, specifically because while the System.Drawing approach does work, it is unfortunately based on GDI+ (note the plus), which is Microsoft's "less than ideal" managed wrapper around GDI32, and the GDI+ Bitmap has really odd issues with stability and resource usage. I'd use stronger language but pretty sure that's forbidden here. So the answer is to use WPF's version (DirextX -based), which is the better implementation in my experience.
    – Chris O
    Dec 28, 2022 at 0:15
2

always Working with regions ( rectangle ) is much faster Than using individual pixels.

1

Creating bitmap object bmp of Size s (height , width) and Color c.

bmp = CreateBmp(c, s);

Now CreateBmp method which returns bitmap:

Bitmap CreateBmp(Color c, Size s)
{
    Bitmap temp =new Bitmap(1, 1);
    temp.SetPixel(0, 0, c);
    return new Bitmap(temp, s);
}
-2

I suggest checking out the GD Library.

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

1
  • This is certainly overkill for the OP's question. May 1, 2022 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.