繁体   English   中英

从列表框项创建位图图像

[英]Create a Bitmap Image from a Listbox items

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

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

它可以工作,并且仅对某些项目进行位图绘制,但是我想对所有项目进行位图绘制(这意味着存在于listbox中的所有项目)。

我该如何解决? 提前致谢。

您需要至少暂时将“列表框高度”调整为显示所有项目所需的大小。

这是高度的代码:

    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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM