1
在我的项目中,我需要从磁盘检索自定义图像文件。如果图像文件不存在于所提供的路径中,则应用程序将使用默认图像(嵌入资源)。一旦我有了图像,我需要调整它的大小以便在应用程序中使用。从资源文件访问图像对象
如果我只尝试访问嵌入式资源(代码部分1),一切都按预期工作。如果尝试将图像(代码段2)的条件,对象回来与对象上的所有类型的异常,最值得注意的是,我的目的:
((System.Drawing.Image)(ReportLogoToUse)).Height' threw an exception of type 'System.ArgumentException' int {System.ArgumentException}
((System.Drawing.Image)(ReportLogoToUse)).Width' threw an exception of type 'System.ArgumentException' int {System.ArgumentException}
这里是我的代码
// Code Section 1
using (var myImage = Resources.sie_logo_petrol_rgb){
// resize the image to max allowed dimensions (64 x 233)
var resizedImage = Helpers.ResizeImage(myImage, (int)maxWidth, (int)maxHeight); // this code executes with no errors
pictureBox1.Image = resizedImage;
}
// Code Section 2
using (var ReportLogoToUse = Helpers.ReportLogo(filePath)){
// resize the image to max allowed dimensions (64 x 233)
var resizedImage = Helpers.ResizeImage(ReportLogoToUse, (int)maxWidth, (int)maxHeight); // Invalid Parameter error
pictureBox2.Image = resizedImage;
}
public static Bitmap ReportLogo(string filePath){
try{
var myImage = Image.FromFile(filePath, true);
return (Bitmap)myImage;
}
catch (Exception ex){
// use the embedded logo
using (var myResourceImage = Resources.sie_logo_petrol_rgb){
var myImage = myResourceImage;
return (Bitmap)myImage;
}
}
}
代码部分1和代码部分2中的对象之间有什么区别?他们不是返回同一种对象吗?
也许程序试图调整资源的图像大小。看看有没有像'新的位图(Helpers.ReportLogo(filePath))'。 – null
@null:在使用'Helpers.ResizeImage'函数之前,问题出在ReportLogoToUse对象 – Kulstad
这里调试'返回(位图)myImage;''try'和'catch'都能在任何方法?要么重新投入资源,要么投射它。也尝试删除'使用(var myResourceImage ...' – null