欢迎光临|狼窝|!!!

爱伦狼

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

用到某处要使用自定义颜色填充图像,貌似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.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);
}

 

参考文章:http://www.csharpwin.com/csharpspace/9115r3566.shtml

 

 

posted on 2010-04-22 23:28  爱伦狼  阅读(15211)  评论(0编辑  收藏  举报
  征婚:

自我描述:年方三八,有车有房,相貌堂堂,工作稳定,尚未婚配。

条件:人类,女性,未婚。

待遇:应征者:烤香肠一根。征用者:钻戒一枚。应征后试征用期间,花费酌情予以报销。

备注:只要爱情不要房子的女孩有绝对优先权。(90后、非主流、类非主流可以忽略)

(你看见说明你很有缘O(∩_∩)O~)