日期:2014-05-19  浏览次数:20994 次

急!!高分求油漆桶实现的代码!!
我起初用getpixles和setpixles的方法实现,但是处理太太太慢了。
后来用floodfill,可是函数返回值为1,说明运行成功,但是不知道为什么图像没有反应。
求高手给段实现代码。。我实在没办法了--!!
分不够再加.

------解决方案--------------------
一定是图形学作业 当初我也做过 全部是用C+WINAPI实现的
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
FloodFill(g.GetHdc(), e.X,e.Y, 0x000000FF);
g.ReleaseHdc();
g.Dispose();
}
注意设置current brush
------解决方案--------------------
//参考http://new.aspnetmania.com/Forums/ForumMessage/196936.html

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));
FloodFillHelper.FloodFill(vGraphics, new Point(10, 10),
FloodFillHelper.GetPixel(vGraphics, 10, 10), Color.Red);
}

public class FloodFillHelper
{
[DllImport( "Gdi32.dll ")]
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport( "Gdi32.dll ")]
private static extern IntPtr CreateSolidBrush(int crColor);
[DllImport( "gdi32 ", CharSet = CharSet.Auto)]
private static extern bool ExtFloodFill(IntPtr hDC, int x, int y, int сolorRefColor, FillType fillType);
[DllImport( "gdi32.dll ", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
[DllImport( "GDI32 ", CharSet = CharSet.Auto)]
private static extern int GetPixel(IntPtr hdc, int x, int y);
private enum FillType
{
FLOODFILLBORDER = 0,
FLOODFILLSURFACE = 1
}
public static void FloodFill(Graphics g, Point p, Color src, Color trg)
{
FloodFill(g, p.X, p.Y, src, trg);
}
public static void FloodFill(Graphics g, int x, int y, Color src, Color trg)
{
IntPtr hdc = g.GetHdc();
IntPtr hBrush = CreateSolidBrush(ColorTranslator.ToWin32(trg));
IntPtr hPreviouseBrush = SelectObject(hdc, hBrush);
System.Diagnostics.Trace.WriteLine(ExtFloodFill(hdc, x, y, ColorTranslator.ToWin32(src), FillType.FLOODFILLSURFACE));
SelectObject(hdc, hPreviouseBrush);
DeleteObject(hBrush);
g.ReleaseHdc(hdc);
}
public static Color GetPixel(Graphics g, int x, int y)
{
IntPtr hdc = g.GetHdc();
int cr = GetPixel(hdc, x, y);
g.ReleaseHdc(hdc);
return ColorTranslator.FromWin32(cr);
}
public static Color GetPixel(Image img, int x, int y)
{
using (Graphics g = Graphics.FromImage(img))
{
IntPtr hdc = g.GetHdc();
int cr = GetPixel(hdc, x, y);
g.ReleaseHdc(hdc);
return ColorTranslator.FromWin32(cr);
}
}
}