Search code examples
c#vb6vb6-migration

FloodFill using C#


I am trying to rewrite some old vb6 code using C#. The problem is that when I used FloodFill in vb it saves the image with the affect of FloodFill. This is not true using C#. Here is the code segment for 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#

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.


Solution

  • I found the answer. I have to use BitBlt so every thing appears on the control surface is saved.

    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");