一个图像和一些文本到ListBox

时间:2012-06-18 10:23:50

标签: c# winforms

我花了很多时间来解决我的问题。 我已经尝试了很长时间在图像下面绘制一个图像和一些文本。我的代码有什么问题。我看不出错误。 我的自定义控制代码是:

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
                }
            }
        }
    }
}

我需要ListBox Image和文字作为收据。

亲切的问候,

1 个答案:

答案 0 :(得分:0)

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