这个问题让我对很多事情感到困惑。
这是一个从 a 写入图像文件的解决方案Panel
相当大的尺寸..
限制因素之一是生成的位图的大小。我测试过的尺寸可达12.5k * 25k
并发现它工作正常;不过,尺寸可能取决于您的机器。我认为你需要相当多的连续内存来创建这么大的内存Bitmap
.
另一个问题是,正如你的标题所示,确实与DrawToBitmap
方法本身。看起来它似乎无法可靠地写入大型位图,这就是为什么我必须将其结果缓冲在临时位图中。如果控件的任何尺寸超过某个大小(也许是 4k,但也许不是),它也无法工作。
该解决方案首先创建一个Bitmap
of the Panel
的尺寸。然后它创建一个临时的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
!