【问题标题】:FloodFill using c-sharp使用 c-sharp 进行洪水填充
【发布时间】:2020-12-25 20:55:17
【问题描述】:

我正在尝试使用 C-sharp 重写一些旧的 vb6 代码。问题是当我在 vb 中使用 FloodFill 时,它会保存具有 FloodFill 影响的图像。使用升 C 时,情况并非如此。这是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

这里是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);

感谢任何帮助。

【问题讨论】:

  • 您正在使用控件的 hDC,然后尝试保存分配给其 Image 属性的位图。这两者没有任何共同点(除了控件本身)。使用位图的 hDC 并对其进行填充。顺便说一句,您应该使用ExtFloodFill(这是一个真正的不容忍 功能 - 作为它的旧版本 - 并给出非常糟糕的结果)。除非您不关心,否则您应该尝试手动执行此操作,使用定义区域或边框的颜色的亮度。添加容差,以保留抗锯齿区域。
  • 您还可以选择在控件的表面上绘制图像 - 在控件的 Paint 事件中 - 将控件 hDC 用于ExtFloodFill 函数 (@987654325 @),在[Control].DrawToBitmap() 之前调用e.Graphics.Flush()。当然,您不需要将 Bitmap 分配给 Image 属性。
  • 感谢吉姆的回复。我尝试了 flush 然后 DrawToBitmap() ,我得到了相同的结果。然而,在搜索 DrawToBitmap 的过程中,我找到了问题的答案,那就是 BitBlt。
  • Control.DrawToBitmap() 确实使用 BitBlt。但是,如前所述,您必须使用由 Paint 事件的 PaintEventArgs 对象提供的 Graphics 对象:这里是 hDC 引用。你用错了。

标签: c# vb6 vb6-migration


【解决方案1】:

我找到了答案。我必须使用 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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多