图像未出现在所选项目组合框中
Image does not appear in selected item combobox
我创建了一个显示图像的组合框,但所选项目的图像没有出现。
我从路径获取图像并将它们保存在一个数组中。这是代码:
private void popolaDivise()
{
Image[] images = System.IO.Directory.GetFiles("divise\")
.Select(file => System.Drawing.Image.FromFile(file))
.ToArray();
cmbDivisaGruppo.DisplayImages(images);
cmbDivisaGruppo.DropDownHeight = 200;
}
displayImages是自定义class的一个方法叫ComboBoxTools,我报代码:
public static class ComboBoxTools
{
// Margins around owner drawn ComboBoxes.
private const int MarginWidth = 4;
private const int MarginHeight = 4;
#region Display Images
// Set up the ComboBox to display images.
public static void DisplayImages(this ComboBox cbo, Image[] images)
{
// Make the ComboBox owner-drawn.
cbo.DrawMode = DrawMode.OwnerDrawVariable;
// Add the images to the ComboBox's items.
//cbo.Items.Clear();
foreach (Image image in images) cbo.Items.Add(image);
// Subscribe to the DrawItem event.
cbo.MeasureItem += cboDrawImage_MeasureItem;
cbo.DrawItem += cboDrawImage_DrawItem;
}
// Measure a ComboBox item that is displaying an image.
private static void cboDrawImage_MeasureItem(object sender, MeasureItemEventArgs e)
{
if (e.Index < 0) return;
// Get this item's image.
ComboBox cbo = sender as ComboBox;
Image img = (Image)cbo.Items[e.Index];
e.ItemHeight = img.Height + 2 * MarginHeight;
e.ItemWidth = img.Width + 2 * MarginWidth;
}
// Draw a ComboBox item that is displaying an image.
private static void cboDrawImage_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
// Clear the background appropriately.
e.DrawBackground();
// Draw the image.
ComboBox cbo = sender as ComboBox;
Image img = (Image)cbo.Items[e.Index];
int hgt = e.Bounds.Height - 2 * MarginHeight;
float scale = hgt / img.Height;
float wid = img.Width * scale;
RectangleF rect = new RectangleF(
e.Bounds.X + MarginWidth,
e.Bounds.Y + MarginHeight,
wid, hgt);
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.DrawImage(img, rect);
// Draw the color's name to the right.
using (Font font = new Font(cbo.Font.FontFamily,
cbo.Font.Size * 0.85f))
{
using (StringFormat sf = new StringFormat())
{
string[] colori = new string[] { "Nero", "Rosso", "Bianco", "Arancione", "Giallo", "Verde", "Azzurro", "Verde chiaro", "Blu", "Blu scuro" };
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
int x = hgt + 2 * MarginWidth;
int y = e.Bounds.Y + e.Bounds.Height / 2;
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
e.Graphics.DrawString(colori[e.Index], font,
Brushes.Black, x, y, sf);
}
}
// Draw the focus rectangle if appropriate.
e.DrawFocusRectangle();
}
#endregion Display Images
}
如何显示与所选项目关联的图像?
您可以使用以下方法在您的组合框中查找事件:
ComboBoxSelectedIndexChanged()
{
// find image of selected index by parsing the name :
switch(Combobox.selectedValue)
{
case "Nero": …
case "Rosso": …
// draw your image here
}
您必须为该函数定义一个名称并将其绑定到 ComboBox.SelectedIndexChanged 事件。
我创建了一个显示图像的组合框,但所选项目的图像没有出现。
我从路径获取图像并将它们保存在一个数组中。这是代码:
private void popolaDivise()
{
Image[] images = System.IO.Directory.GetFiles("divise\")
.Select(file => System.Drawing.Image.FromFile(file))
.ToArray();
cmbDivisaGruppo.DisplayImages(images);
cmbDivisaGruppo.DropDownHeight = 200;
}
displayImages是自定义class的一个方法叫ComboBoxTools,我报代码:
public static class ComboBoxTools
{
// Margins around owner drawn ComboBoxes.
private const int MarginWidth = 4;
private const int MarginHeight = 4;
#region Display Images
// Set up the ComboBox to display images.
public static void DisplayImages(this ComboBox cbo, Image[] images)
{
// Make the ComboBox owner-drawn.
cbo.DrawMode = DrawMode.OwnerDrawVariable;
// Add the images to the ComboBox's items.
//cbo.Items.Clear();
foreach (Image image in images) cbo.Items.Add(image);
// Subscribe to the DrawItem event.
cbo.MeasureItem += cboDrawImage_MeasureItem;
cbo.DrawItem += cboDrawImage_DrawItem;
}
// Measure a ComboBox item that is displaying an image.
private static void cboDrawImage_MeasureItem(object sender, MeasureItemEventArgs e)
{
if (e.Index < 0) return;
// Get this item's image.
ComboBox cbo = sender as ComboBox;
Image img = (Image)cbo.Items[e.Index];
e.ItemHeight = img.Height + 2 * MarginHeight;
e.ItemWidth = img.Width + 2 * MarginWidth;
}
// Draw a ComboBox item that is displaying an image.
private static void cboDrawImage_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
// Clear the background appropriately.
e.DrawBackground();
// Draw the image.
ComboBox cbo = sender as ComboBox;
Image img = (Image)cbo.Items[e.Index];
int hgt = e.Bounds.Height - 2 * MarginHeight;
float scale = hgt / img.Height;
float wid = img.Width * scale;
RectangleF rect = new RectangleF(
e.Bounds.X + MarginWidth,
e.Bounds.Y + MarginHeight,
wid, hgt);
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.DrawImage(img, rect);
// Draw the color's name to the right.
using (Font font = new Font(cbo.Font.FontFamily,
cbo.Font.Size * 0.85f))
{
using (StringFormat sf = new StringFormat())
{
string[] colori = new string[] { "Nero", "Rosso", "Bianco", "Arancione", "Giallo", "Verde", "Azzurro", "Verde chiaro", "Blu", "Blu scuro" };
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
int x = hgt + 2 * MarginWidth;
int y = e.Bounds.Y + e.Bounds.Height / 2;
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
e.Graphics.DrawString(colori[e.Index], font,
Brushes.Black, x, y, sf);
}
}
// Draw the focus rectangle if appropriate.
e.DrawFocusRectangle();
}
#endregion Display Images
}
如何显示与所选项目关联的图像?
您可以使用以下方法在您的组合框中查找事件:
ComboBoxSelectedIndexChanged()
{
// find image of selected index by parsing the name :
switch(Combobox.selectedValue)
{
case "Nero": …
case "Rosso": …
// draw your image here
}
您必须为该函数定义一个名称并将其绑定到 ComboBox.SelectedIndexChanged 事件。