这个问题让我对很多事情感到困惑..
这是一个从相当大的Panel 写入图像文件的解决方案..
其中一个限制因素是生成的位图的大小。我已经测试了高达12.5k * 25k 的大小,发现它工作正常;不过,尺寸可能取决于您的机器。我认为您需要相当多的连续内存才能创建如此大的Bitmap。
另一个问题是,正如您的标题所暗示的,确实与 DrawToBitmap 方法本身有关。它看起来好像不能可靠地写入大型位图,这就是为什么我必须将其结果缓冲在一个临时位图中。如果控件的任何维度超过某个大小(可能是 4k,但可能不是),它也无法工作。
解决方案首先创建Panel 大小的Bitmap。然后它会创建一个临时的Panel 来容纳大的Panel。这个容器足够小,可以让DrawToBitmap 工作。
然后它在宽度和高度上循环,向上和向左移动大Panel,粘贴DrawToBitmap带回来的部分,逐步进入大Bitmap。
最后它写回PNG以获得最佳可读性和大小..
private void button2_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(largePanel.ClientSize.Width, largePanel.ClientSize.Height);
DrawToBitmap(largePanel, bmp); // the patchwork method
bmp.Save(yourFileName, System.Drawing.Imaging.ImageFormat.Png);
bmp.Dispose(); // get rid of the big one!
GC.Collect(); // not sure why, but it helped
}
void DrawToBitmap(Control ctl, Bitmap bmp)
{
Cursor = Cursors.WaitCursor; // yes it takes a while
Panel p = new Panel(); // the containing panel
Point oldLocation = ctl.Location; //
p.Location = Point.Empty; //
this.Controls.Add(p); //
int maxWidth = 2000; // you may want to try other sizes
int maxHeight = 2000; //
Bitmap bmp2 = new Bitmap(maxWidth, maxHeight); // the buffer
p.Height = maxHeight; // set up the..
p.Width = maxWidth; // ..container
ctl.Location = new Point(0, 0); // starting point
ctl.Parent = p; // inside the container
p.Show(); //
p.BringToFront(); //
// we'll draw onto the large bitmap with G
using (Graphics G = Graphics.FromImage(bmp))
for (int y = 0; y < ctl.Height; y += maxHeight)
{
ctl.Top = -y; // move up
for (int x = 0; x < ctl.Width; x += maxWidth)
{
ctl.Left = -x; // move left
p.DrawToBitmap(bmp2, new Rectangle(0, 0, maxWidth, maxHeight));
G.DrawImage(bmp2, x, y); // patch together
}
}
ctl.Location = p.Location; // restore..
ctl.Parent = this; // form layout <<<==== ***
p.Dispose(); // clean up
Cursor = Cursors.Default; // done
}
我在Panel 上画了一些东西,然后扔了几百个Buttons,结果看起来很无缝。无法发布,原因很明显..
*** 注意:如果您的面板不在表单上,您应该将this 更改为真正的Parent!