Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create 1024x1024 RGB bitmap image of white?

Tags:

c#

It's embarrassing to ask this question but can't find an answer.

I tried this in vain.

Image resultImage = new Bitmap(image1.Width, image1.Height, PixelFormat.Format24bppRgb);

using (Graphics grp = Graphics.FromImage(resultImage)) 
{
    grp.FillRectangle(
        Brushes.White, 0, 0, image1.Width, image1.Height);
    resultImage = new Bitmap(image1.Width, image1.Height, grp);
}

I basically want to fill a 1024x1024 RGB bitmap image with white in C#. How can I do that?

like image 596
Tae-Sung Shin Avatar asked Sep 19 '12 20:09

Tae-Sung Shin


3 Answers

You almost had it:

private Bitmap DrawFilledRectangle(int x, int y)
{
    Bitmap bmp = new Bitmap(x, y);
    using (Graphics graph = Graphics.FromImage(bmp))
    {
        Rectangle ImageSize = new Rectangle(0,0,x,y);
        graph.FillRectangle(Brushes.White, ImageSize);
    }
    return bmp;
}
like image 101
Lee Harrison Avatar answered Nov 15 '22 00:11

Lee Harrison


You are assigning a new image to resultImage, thereby overwriting your previous attempt at creating a white image (which should succeed, by the way).

So just remove the line

resultImage = new Bitmap(image1.Width, image1.Height, grp);
like image 34
Joey Avatar answered Nov 15 '22 00:11

Joey


Another approach,

Create a unit bitmap

var b = new Bitmap(1, 1);
b.SetPixel(0, 0, Color.White);

And scale it

var result = new Bitmap(b, 1024, 1024);
like image 23
prashanth Avatar answered Nov 14 '22 23:11

prashanth