当前位置:编程学习 > C#/ASP.NET >>

C#用自定义或指定颜色填充矩形

貌似Graphics.FillRectangle方法只能用Brush来填充。

GOOGLE了下有两种方法:
 
一、用线性渐变填充

 

public void FillByColor(Rectangle rect,Color c,Graphics G)
{
System.Drawing.Drawing2D.LinearGradientBrush lBrush = new System.Drawing.Drawing2D.LinearGradientBrush(rect, c, c, 0);
G.FillRectangle(lBrush, rect);
}


参考文章:《如何:创建线性渐变》

 
 
 

二、用API实现指定颜色填充一个闭合区域(未测试)
\用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);
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,