簡體   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