Search code examples
c#drawingsystem.drawing

C# Custom color turning black


I've never used System.Drawing before so bear with me as this is a mich-mach of code I've found and put together.

I'm trying to create an image with a top portion white and bottom portion another color (eventually adding images into it)

my code:

        Bitmap bmp = new Bitmap(800, 800, PixelFormat.Format32bppArgb);
        using (Graphics gfx = Graphics.FromImage((Image)bmp))
        {
            gfx.FillRectangle(Brushes.Transparent, new RectangleF(0, 0, bmp.Width, bmp.Height));
            gfx.FillRectangle(Brushes.White, 0, 0, 800, 600);
            gfx.FillRectangle(new SolidBrush(Color.FromArgb(21, 93, 127)), 600, 800, 0, 800);
        }
        bmp.Save("C:\test.jpg", ImageFormat.Bmp);

However my results are top white, bottom black... not sure what I'm doing wrong.

I've also tried gfx.FillRectangle(new SolidBrush(System.Drawing.ColorTranslator.FromHtml("#155d7f")), 600, 800, 0, 800);


Solution

  • gfx.FillRectangle(new SolidBrush(Color.FromArgb(21, 93, 127)), 600, 800, 0, 800);
    

    You are drawing a rectangle from the coordinate 600,800, on a total width of 0px and height of 800px, as you can see from MSDN here. Because the area is left uncolored, it appears black.

    I think you meant to do :

    gfx.FillRectangle(new SolidBrush(Color.FromArgb(21, 93, 127)), 0, 600, 800, 200);