简体   繁体   English

从列表框项创建位图图像

[英]Create a Bitmap Image from a Listbox items

I want to create a bitmap image from a listbox . 我想从列表框创建位图图像。 I can use this : 我可以用这个:

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

It works and only bitmap some items , but I want to bitamap all the items (It mean all the items that exists in listbox ). 它可以工作,并且仅对某些项目进行位图绘制,但是我想对所有项目进行位图绘制(这意味着存在于listbox中的所有项目)。

How can I solve it? 我该如何解决? Thanks in advance. 提前致谢。

You need to adjust the Listbox Height at least temporarily, to the size needed to show all Items. 您需要至少暂时将“列表框高度”调整为显示所有项目所需的大小。

Here is the code for the 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;

you may want to do some checks on the Width as well.. 您可能还需要对宽度进行一些检查。

For decent text output I recoommend saving to PNG. 对于体面的文本输出,我建议保存为PNG。

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

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