用到某處要使用自訂顏色填滿影像,看起來像Graphics.FillRectangle方法只能用Brush來填滿。但是常規Brush都是Brushes定義好的Brush,怎麼辦呢?
GOOGLE了下有兩種方法:
一、用新SolidBrush填充
public void FillByColor(Rectangle rect,Color c,Graphics G){ G.FillRectangle(new SolidBrush(c), rect);}
參考文章:《如何:創造線性漸層》
二、用API實作指定顏色填滿一個閉合區域(未測試)
用API實作指定顏色填充
using System.Runtime.InteropServices;[DllImport("gdi32.dll")]public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);[DllImport("gdi32.crll")]public static Soltern IntrushPate(PateD)]public static Soltern) [DllImport("gdi32.dll")]public static extern bool ExtFloodFill(IntPtr hdc, int nXStart, int nYStart, int crColor, uint fuFillType);[DllImport("gdi32.dll")) );[DllImport("gdi32.dll")]public static extern int GetPixel(IntPtr hdc, int x, int y);public static uint FLOODFILLBORDER = 0;public static uint FLOODFILLSURFACE = 1; e){ Graphics vGraphics = Graphics.FromHwnd(Handle); vGraphics.DrawRectangle(Pens.Blue, new Rectangle(0, 0, 300, 300)); vGraphics.DrawRectangle(Pens.Blue, new Rectangle(50, 70, 30070, 30070, 30070, 30070, 30070, 300 , 300)); IntPtr vDC = vGraphics.GetHdc(); IntPtr vBrush = CreateSolidBrush(ColorTranslator.ToWin32(Color.Red)); IntPtr vPreviouseBrush = SelectObject(vDC, vBrush); ExtFloodFill(DDCFl, FDC, 106, 105, , 10, 10), FLOODFILLSURFACE); SelectObject(vDC, vPreviouseBrush); DeleteObject(vBrush); vGraphics.ReleaseHdc(vDC);}
參考文章: http://www.csharpwin.com/csharpspace/9115r3566.shtml
(本文來源: http://www.cnblogs.com/allanswolf/archive/2010/04/22/1718217.html )