保存生成的缩略图会使对象引用未设置异常

时间:2015-10-25 00:27:41

标签: 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 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);

2 个答案:

答案 0 :(得分:-1)

完成工作后,您必须Dispose Graphics。当您使用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);

答案 1 :(得分:-1)

问题是你的方法中有一个try..catch,这是吞噬异常,你从那里返回null,因此你报告了异常。

选项1 我的建议是要么做一些异常,例如记录它,这样你知道出了什么问题,就像现在一样。并处理该方法可以返回null值的实例,如下所示:

    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并按如下方式将其移出:

注意:以下代码假定您已从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
}