You need to fill the image with a custom color somewhere. It seems that the Graphics.FillRectangle method can only be filled with Brush. But the regular Brush is the Brush defined by Brushes, what should I do?
There are two ways to GOOGLE:
1. Fill with new SolidBrush
public void FillByColor(Rectangle rect,Color c,Graphics G){ G.FillRectangle(new SolidBrush(c), rect);}
Reference article: "How to: Create a Linear Gradient"
2. Use API to fill a closed area with specified color (not tested)
Implement specified color filling using API
using System.Runtime.InteropServices;[DllImport("gdi32.dll")]public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);[DllImport("gdi32.dll")]public static extern IntPtr CreateSolidBrush(int crColor); [DllImport("gdi32.dll")]public static extern bool ExtFloodFill(IntPtr hdc, int nXStart, int nYStart, int crColor, uint fuFillType);[DllImport("gdi32.dll")]public static extern bool DeleteObject(IntPtr hObject );[DllImport("gdi32.dll")]public static extern int GetPixel(IntPtr hdc, int x, int y);public static uint FLOODFILLBORDER = 0;public static uint FLOODFILLSURFACE = 1;private void button1_Click(object sender, EventArgs e){ Graphics vGraphics = Graphics.FromHwnd(Handle); vGraphics.DrawRectangle(Pens.Blue, new Rectangle(0, 0, 300, 300)); vGraphics.DrawRectangle(Pens.Blue, new Rectangle(50, 70, 300) , 300)); IntPtr vDC = vGraphics.GetHdc(); IntPtr vBrush = CreateSolidBrush(ColorTranslator.ToWin32(Color.Red)); IntPtr vPreviouseBrush = SelectObject(vDC, vBrush); ExtFloodFill(vDC, 10, 10, GetPixel(vDC , 10, 10), FLOODFILLSURFACE); SelectObject(vDC, vPreviouseBrush); DeleteObject(vBrush); vGraphics.ReleaseHdc(vDC);}
Reference article: http://www.csharpwin.com/csharpspace/9115r3566.shtml
(Source of this article: http://www.cnblogs.com/allanswolf/archive/2010/04/22/1718217.html )