我正在尝试使用 C# 重写一些旧的 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);
如有任何帮助,我们将不胜感激。
我找到了答案。我必须使用 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");