简体   繁体   中英

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 ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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