gpt4 book ai didi

c# - 一个图像和一些文本到 ListBox

转载 作者:太空宇宙 更新时间:2023-11-03 16:30:16 26 4
gpt4 key购买 nike

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

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 和 Text 的 ListBox

亲切的问候,

可爱

最佳答案

如果你想使用来自父类的可绘制文本移动 base.OnDrawItem(e);OnDrawItem 的末尾,因为你清除了这个文本。而且,也许,您需要在绘制图像后将“DrawMode.OwnerDrawFixed”关闭为“DrawMode.Normal”或手动绘制字符串(如 g.DrawString(GetText(),...) )

关于c# - 一个图像和一些文本到 ListBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11080919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com