C# 保存生成的缩略图会导致对象引用未设置异常

C# 保存生成的缩略图会导致对象引用未设置异常,c#,thumbnails,C#,Thumbnails,如何保存生成的缩略图?我得到这个错误: 对象引用未设置为对象的实例 这是我的密码。我是C#的新手。我在网上找到了缩略图生成代码,我想我可以使用它,但它给了我一个错误 //1. <lcFilename> as path of large size file. //2. <lnWidth> as width of required thumbnail. //3. <lnHeight> as height of required thumbnail. //The f

如何保存生成的缩略图?我得到这个错误:

对象引用未设置为对象的实例

这是我的密码。我是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);
//1。作为大文件的路径。
//2.  作为所需缩略图的宽度。
//3.  作为所需缩略图的高度。
//该函数返回可保存在磁盘上的已更改缩略图的位图对象。
公共静态位图创建缩略图(字符串lcFilename、int lnWidth、int lnHeight)
{
System.Drawing.Bitmap bmpOut=null;
尝试
{
位图loBMP=新位图(lcFilename);
ImageFormat loFormat=loBMP.RawFormat;
小数比例;
int lnNewWidth=0;
int lnNewHeight=0;
//***如果图像小于缩略图,请返回它
if(loBMP.WidthloBMP.Height)
{
lnRatio=(十进制)lnWidth/loBMP.Width;
lnNewWidth=lnWidth;
十进制LNTTEMP=叶高*LNTRATIO;
lnNewHeight=(int)lnTemp;
}
其他的
{
lnRatio=(十进制)lnHeight/loBMP.Height;
lnNewHeight=lnHeight;
十进制LNTTEMP=叶宽*LNTRATIO;
lnNewWidth=(int)lnTemp;
}
bmpOut=新位图(lnNewWidth、lnNewHeight);
Graphics g=Graphics.FromImage(bmpOut);
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g、 FillRectangle(画笔:白色、0、0、lnNewWidth、lnNewHeight);
g、 DrawImage(loBMP、0、0、lnNewWidth、lnNewHeight);
loBMP.Dispose();
}
抓住
{
返回null;
}
返回bmpOut;
}
//缩略图生成
字符串largefilepath=“images/”+imageupload;
字符串largefilepath2=“images/users/”+imageupload+“-160x160”;
位图bmp1=新位图(创建缩略图(大文件路径,160160));
bmp1.Save(大文件路径2);

您必须在
完成其工作后处理
图形。当您使用
图形
在图像上绘制某些内容时,通常使用
使用
块,但由于它已经在
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);
//1。作为大文件的路径。
//2.  作为所需缩略图的宽度。
//3.  作为所需缩略图的高度。
//该函数返回可保存在磁盘上的已更改缩略图的位图对象。
公共静态位图创建缩略图(字符串lcFilename、int lnWidth、int lnHeight)
{
System.Drawing.Bitmap bmpOut=null;
尝试
{
位图loBMP=新位图(lcFilename);
ImageFormat loFormat=loBMP.RawFormat;
小数比例;
int lnNewWidth=0;
int lnNewHeight=0;
//***如果图像小于缩略图,请返回它
if(loBMP.WidthloBMP.Height)
{
lnRatio=(十进制)lnWidth/loBMP.Width;
lnNewWidth=lnWidth;
十进制LNTTEMP=叶高*LNTRATIO;
lnNewHeight=(int)lnTemp;
}
其他的
{
lnRatio=(十进制)lnHeight/loBMP.Height;
lnNewHeight=lnHeight;
十进制LNTTEMP=叶宽*LNTRATIO;
lnNewWidth=(int)lnTemp;
}
bmpOut=新位图(lnNewWidth、lnNewHeight);
Graphics g=Graphics.FromImage(bmpOut);
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g、 FillRectangle(画笔:白色、0、0、lnNewWidth、lnNewHeight);
g、 DrawImage(loBMP、0、0、lnNewWidth、lnNewHeight);
//处理图形,使其释放在该图像上绘制所需的所有资源。
g、 处置();
/*或者您可以使用下面的图形..但它似乎是多余的,因为它已经在try/catch块中。
使用(Graphics g=Graphics.FromImage(bmpOut))
{
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g、 FillRectangle(画笔:白色、0、0、lnNewWidth、lnNewHeight);
g、 DrawImage(loBMP、0、0、lnNewWidth、lnNewHeight);
}
*/
loBMP.Dispose();
}
抓住
{
返回null;
}
返回bmpOut;
}
//缩略图生成
字符串largefilepath=“images/”+imageupload;
字符串largefilepath2=“images/users/”+imageupload+“-160x160”;
位图bmp1=新位图(创建缩略图(大文件路径,160160));
bmp1.Save(大文件路径2);

问题在于,您在方法中有一个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,并按如下方式将其移出:

    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);
    }
注意:下面的代码假设您已经从CreateThumbnail方法中删除了try…catch

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
}

您需要使用适当的XML注释来记录您的方法。在哪一行出现这样的错误?我尝试了第二个选项,并收到消息“Parameter is not valid”…尝试了调试,但仍然无法指出问题所在…请帮助这是我在c#中的第一个项目…感谢您的帮助我已经更新了示例代码,它可以工作了。原著