从列表框项创建位图图像

本文关键字:位图 图像 创建 列表 | 更新日期: 2023-09-27 18:14:21

我想从列表框创建一个位图图像。我可以这样写:

Bitmap bmp = new Bitmap(this.listBox1.Width, this.listBox1.Height);
this.listBox1.DrawToBitmap(bmp, this.listBox1.ClientRectangle);
bmp.Save(@"Data.jpg");

它只工作和位图一些项目,但我想要位图所有的项目(这意味着所有的项目存在于列表框)。

我怎么解决它?

从列表框项创建位图图像

你需要调整列表框的高度,至少暂时调整到显示所有项目所需的大小。

下面是Height的代码:
    int oh = listBox1.Height;
    listBox1.Height = listBox1.ItemHeight * listBox1.Items.Count
                   + (listBox1.Height - listBox1.ClientSize.Height);
    Bitmap bmp = new Bitmap(this.listBox1.Width, this.listBox1.Height);
    this.listBox1.DrawToBitmap(bmp, this.listBox1.ClientRectangle);
    bmp.Save(@"Data.png" , System.Drawing.Imaging.ImageFormat.Png);
    listBox1.Height = oh;

你可能还想对宽度做一些检查…

对于体面的文本输出,我建议保存为PNG。