我有一个WinForms应用程序,我使用代码在按钮上显示图像。图像对应于填充表单的记录。当加载或刷新表单数据时,它使用记录中的键值,并且应该显示从与记录关联的jpg创建的缩略图。它抓住了正确的jpg;但是,创建缩略图时,它会在按钮上显示错误的图像。问题不会出现在所有文件上,只有一些。它不能使用图像列表,因为图像是可编辑的,目前有8,000条记录和相应的图像。
任何人都对JPG中要查找的内容有所了解,或者我应该使用不同的控件类型?
提前谢谢。
//Method called when form data is loaded or refreshed
private void RefreshImage()
{
try
{
//Clear any current image
btnViewImage.Image = null;
string strNum = RecordDetailBDL.Data[KeyId].ToString();
//Need to figure out how to store in and retrieve from the database
string strDirectory = ConfigurationManager.AppSettings["ImageDirectoryLocation"];
string defaultImage = ConfigurationManager.AppSettings["ImageDefaultFile"];
string fileName = string.Empty;
if (System.IO.Directory.GetFiles(strDirectory, strNum + ".jpg").Length > 0)
{
fileName = string.Format("{0}\\{1}{2}", strDirectory, strNum, ".jpg");
}
else
{
fileName = string.Format("{0}\\{1}", strDirectory, defaultImage);
}
if (fileName != string.Empty)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(fileName, true);
System.Drawing.Image.GetThumbnailImageAbort imgAbort = new System.Drawing.Image.GetThumbnailImageAbort(ImageCallbackAbort);
//Create a thumbnail from the file that is slightly smaller than the button height and width
System.Drawing.Image thumbNail = img.GetThumbnailImage((btnViewImage.Width - 5), (btnViewImage.Height - 5), imgAbort, IntPtr.Zero);
btnViewImage.Image = thumbNail;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:0)
这里的简单解决方法是让我用PictureBox替换控件。使用Load(文件名)方法进行控制时,它会在100%的时间内显示正确的图像。