简体   繁体   English

使用 c-sharp 进行洪水填充

[英]FloodFill using c-sharp

I am trying to rewrite some old vb6 code using C-sharp.我正在尝试使用 C-sharp 重写一些旧的 vb6 代码。 The problem is that when I used FloodFill in vb it saves the image with the affect of FloodFill.问题是当我在 vb 中使用 FloodFill 时,它会保存具有 FloodFill 影响的图像。 This is not true using C-sharp.使用升 C 时,情况并非如此。 Here is the code segment for VB6:这是 VB6 的代码段:

hTempBrush = CreateSolidBrush(&H400000)   
'Select the brush into the dc.
hPrevBrush = SelectObject(Maparea.hdc, hTempBrush)
'Fill the area.
FloodFill Maparea.hdc, (100 ), (200), MapColor
SelectObject Maparea.hdc, hPrevBrush
DeleteObject hTempBrush
SavePicture Maparea.Picture, "filename.bmp" ' saves picture with flood fill affect

and here is the c#这是 c#

Graphics g2 = Graphics.FromHwnd(pictureBox1.handle);
IntPtr vDC = g2.GetHdc();
IntPtr vBrush = CreateSolidBrush(ColorTranslator.ToWin32(Color.Navy));
IntPtr vPreviouseBrush = SelectObject(vDC, vBrush);
int hh = ColorTranslator.ToWin32(Color.Wheat);
FloodFill(vDC, 100, 200, hh);
SelectObject(vDC, vPreviouseBrush);
DeleteObject(vBrush);
pictureBox1.Image.Save("map.bmp");  // saves without the affect of floodfill
g2.ReleaseHdc(vDC);

Any help is appreciated.任何帮助表示赞赏。

I found the answer.我找到了答案。 I have to use BitBlt so every thing appears on the control surface is saved.我必须使用 BitBlt,以便保存控制面上出现的所有内容。

private void button4_Click(object sender, EventArgs e)
{
    var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    using (var bmpGraphics = Graphics.FromImage(bmp))
    {
        var despDC = bmpGraphics.GetHdc();
        using (Graphics formGraphics = Graphics.FromHwnd(pictureBox1.Handle))
        {
            var srcDC = formGraphics.GetHdc();
            BitBlt(despDC, 0, 0, pictureBox1.Width, pictureBox1.Height, srcDC, 0, 0, SRCCOPY);
            formGraphics.ReleaseHdc(srcDC);
        }
        bmpGraphics.ReleaseHdc(despDC);
    }
    bmp.Save("map1.jpg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM