一个图像和一些文本放入列表框中

本文关键字:列表 文本 一个 图像 | 更新日期: 2023-09-27 18:37:28

我花了很多时间来解决这个问题。我尝试了很长时间来绘制一张图像和图像下方的一些文字。我的代码有什么问题。我看不到错误。我的自定义控件代码是:

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ImageListBox
{
    /// <summary>
    /// ImageListBox
    /// </summary>
    public partial class ImageListBox : ListBox
    {
        private Brush _brushText;
        private string _TextMessage;
        public ImageListBox():base()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            const int iWidth = 800;
            const int iHeight = 800;
            PointF ptfCenter = new PointF(iWidth / 2, iHeight / 2);
            base.OnDrawItem(e);
            if (e.Index > -1 && Items.Count > e.Index)
            {
                object itm = Items[e.Index];
                if (itm != null)
                {
                    string fileName = itm.ToString();
                    if (File.Exists(fileName))
                    {
                        Image img = null;
                        try
                        {
                            img = Image.FromFile(fileName);
                            {
                                _brushText = Brushes.Black;
                                Font font = new Font("Arial", 12);
                                Graphics g = e.Graphics;
                                Image img2 = GetThumbnail(img, e.Bounds);
                                //g.DrawString(_TextMessage,font,_brushText,0,img2.Height);
                                g.DrawImageUnscaled(img2, e.Bounds);
                            }
                        }
                        catch (Exception ex)
                        {
                            Graphics g = e.Graphics;
                            var sf = new StringFormat {Trimming = StringTrimming.EllipsisCharacter};
                            string message = string.Format("{0} konnte nicht geladen werden. Exception: {1}", fileName,
                                                           ex);
                            g.DrawString(message, Font, new SolidBrush(ForeColor), e.Bounds, sf);
                        }    
                    }
                }
            }
        }
        private Image GetThumbnail(Image pImage, Rectangle pBounds)
        {
            Font font = new Font("Arial", 12);
            Image ret = new Bitmap(pBounds.Width, pBounds.Height);
            Graphics g = Graphics.FromImage(ret);
            g.FillRectangle(new SolidBrush(Color.White), 0, 0, ret.Width, ret.Height * 10);
            float factor = Math.Max(pImage.Width/(float) pBounds.Width, pImage.Height/(float) pBounds.Height);
            g.DrawImage(pImage, 0, 0, pImage.Width/factor, pImage.Height/factor);
            g.Dispose();
            //g.DrawString("This is a Test a TEST!! ",font, _brushText, 0, pImage.Height);
            return ret;
        }
        public string GetText
        {
            get { return _TextMessage; }
            set { _TextMessage = value; }
        }
    }
}

表格中的测试代码是:

namespace WindowsFormsApplication1
{
    public partial class RibbonForm1 : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public RibbonForm1()
        {
            InitializeComponent();
            //Höhe der Items festlegen (die Breite floatet)
            this.imageListBox1.ItemHeight=200;
            //imageListBox1.GetText = "   Test auf verfügbarkeit   "; // Doesn't work
            //imageListBox1.Items.Add("----------------------------"); // Doesn't work
            //imageListBox1.Items.Add("   Test auf verfügbarkeit   "); // Doesn't work
            //imageListBox1.Items.Add("----------------------------"); // Doesn't work
            string windir = System.Environment.GetEnvironmentVariable("SystemRoot");
            if (windir != null)
            {
                string[] files = System.IO.Directory.GetFiles(windir, "*.bmp");
                foreach (string file in files)
                {
                    imageListBox1.Items.Add(file);
                    //imageListBox1.Items.Add("Text"); // Doesn't work
                }
            }
        }
    }
}

我需要带有Image和文本的ListBox作为收据。

亲切问候

甜蜜

一个图像和一些文本放入列表框中

如果要使用父类中的文本可绘制对象,请将base.OnDrawItem(e);移动到OnDrawItem末尾,因为您可以清除此文本。而且,也许,您需要关闭"DrawMode.OwnerDrawFixed"到"DrawMode.Normal"或在绘制图像后手动绘制字符串(如g.DrawString(GetText(),...)