保存生成的缩略图会导致Object引用未设置异常
本文关键字:Object 引用 异常 设置 略图 保存 | 更新日期: 2023-09-27 18:19:03
如何保存生成的缩略图?我得到这个错误:
对象引用未设置为对象的实例
这是我的代码。我是c#新手。我在网上找到了缩略图生成代码,我以为我可以使用它,但它给了我一个错误…
//1. <lcFilename> as path of large size file.
//2. <lnWidth> as width of required thumbnail.
//3. <lnHeight> as height of required thumbnail.
//The function returns a Bitmap object of the changed thumbnail image which you can save on the disk.
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
//*** If the image is smaller than a thumbnail just return it
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
return loBMP;
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
// Thumbnail Generate
string largefilepath = "images/" + imageuploaded;
string largefilepath2 = "images/users/" + imageuploaded + "-160x160";
Bitmap bmp1 = new Bitmap(CreateThumbnail(largefilepath, 160, 160));
bmp1.Save(largefilepath2);
您必须在Graphics
完成其工作后将其Dispose
。当您使用Graphics
在图像上绘制某些东西时,通常使用using
块,但由于它已经在try/catch
范围内,因此在这里似乎是多余的。
//1. <lcFilename> as path of large size file.
//2. <lnWidth> as width of required thumbnail.
//3. <lnHeight> as height of required thumbnail.
//The function returns a Bitmap object of the changed thumbnail image which you can save on the disk.
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
//*** If the image is smaller than a thumbnail just return it
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
return loBMP;
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
// Dispose Graphics so that it releases all the resources it's holding to draw on that image.
g.Dispose();
/* or you could use Graphics as below.. but it seems redundant, because it is already in the try / catch block.
using ( Graphics g = Graphics.FromImage(bmpOut))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
}
*/
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
// Thumbnail Generate
string largefilepath = "images/" + imageuploaded;
string largefilepath2 = "images/users/" + imageuploaded + "-160x160";
Bitmap bmp1 = new Bitmap(CreateThumbnail(largefilepath, 160, 160));
bmp1.Save(largefilepath2);
问题是你在你的方法中有一个try..catch,这是吞下异常,你从那里返回null,因此你报告的异常。
选项1我的建议是要么对异常做一些事情,比如记录它,这样你就知道出了什么问题,就像现在一样。并处理该方法可以返回空值的实例,如下所示:
string largefilepath = "images/" + imageuploaded;
string largefilepath2 = "images/users/" + imageuploaded + "-160x160";
Bitmap bmp1 = new Bitmap(CreateThumbnail(largefilepath, 160, 160));
if (bmp1 == null) {
// there was an exception, check the logs
}
else {
bmp1.Save(largefilepath2);
}
选项2:或者从方法中完全删除try catch,并将其移出,如下所示:
注意:下面的代码假设你已经删除了try…从CreateThumbnail方法中获取。
try {
//Where imageUploaded is the name of the image with the extension, e.g. "samplePic.jpg"
string largefilepath = @"c:'images'" + imageuploaded;
string largefilepath2 = @"c:'images'users'" + System.IO.Path.GetFileNameWithoutExtension(imageuploaded) + "-160x160" + System.IO.Path.GetExtension(largefilepath);
Bitmap bmp1 = new Bitmap(CreateThumbnail(largefilepath, 160, 160));
bmp1.Save(largefilepath2);
}
catch (Exception ex) {
//do something with the exception
}