Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在组合框中显示图像_C#_.net_Winforms_Combobox - Fatal编程技术网

C# 在组合框中显示图像

C# 在组合框中显示图像,c#,.net,winforms,combobox,C#,.net,Winforms,Combobox,我使用下面的代码在组合框中显示表示颜色的图像,如下面的答案所示 我已经在表单中添加了自定义控件,但是我不知道如何在表单中添加带有图像的项。敬请指教 public sealed class ColorSelector : ComboBox { public ColorSelector() { DrawMode = DrawMode.OwnerDrawFixed; DropDownStyle = ComboB

我使用下面的代码在组合框中显示表示颜色的图像,如下面的答案所示 我已经在表单中添加了自定义控件,但是我不知道如何在表单中添加带有图像的项。敬请指教

  public sealed class ColorSelector : ComboBox
    {
        public ColorSelector()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
            DropDownStyle = ComboBoxStyle.DropDownList;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();

            e.DrawFocusRectangle();

            if (e.Index >= 0 && e.Index < Items.Count)
            {
                DropDownItem item = (DropDownItem)Items[e.Index];

                e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);

                e.Graphics.DrawString(item.Value, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
            }

            base.OnDrawItem(e);
        }
    }

public sealed class DropDownItem
{
    public string Value { get; set; }

    public Image Image { get; set; }

    public DropDownItem()
        : this("")
    { }



 public DropDownItem(string val)
    {
        Value = val;
        Image = new Bitmap(16, 16);
        using (Graphics g = Graphics.FromImage(Image))
        {
            using (Brush b = new SolidBrush(Color.FromName(val)))
            {
                g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
                g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
            }
        }
    }

    public override string ToString()
    {
        return Value;
    }
}
公共密封类颜色选择器:组合框
{
公共颜色选择器()
{
DrawMode=DrawMode.OwnerDrawFixed;
DropDownStyle=ComboBoxStyle.DropDownList;
}
受保护的覆盖无效OnDrawItem(DrawItemEventArgs e)
{
e、 牵引杆接地();
e、 DrawFocusRectangle();
如果(e.Index>=0&&e.Index
根据您的要求,我修改了OnDrawItem方法,并更改了组合框的宽度和高度以清除可见图像

请在下面查找代码

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        if (e.Index >= 0  )
        {
            e.DrawBackground();
            e.DrawFocusRectangle();

            string imageFilePath = @Items[e.Index].ToString();;
            int width = 40;
            int height = 20;

            Image img = Image.FromFile(imageFilePath);
            e.Graphics.DrawImage(img, 0, e.Bounds.Top + 2, width, height);
            e.Graphics.DrawString(imageFilePath, e.Font, new
                    SolidBrush(e.ForeColor), e.Bounds.Left + width, e.Bounds.Top + 2);
            base.OnDrawItem(e);
        }

    }

}
输出:


试试这样的方法:。它是一个标准(平面)组合框,使用ImageList作为图像源。实现起来非常简单。但是,如果你只需要表示一种颜色,你可以选择一种颜色来画一个矩形。请看,仅在动画中,您可以看到用作颜色选择器的组合框。你在寻找这样的结果吗?请检查我的答案