【问题标题】:Compress/Thumbnail Image after uploading file in c# .net在 c# .net 中上传文件后压缩/缩略图图像
【发布时间】:2011-06-13 23:03:23
【问题描述】:

我希望假设用户上传 2 MB 图像,然后从该图像生成 1 个缩略图。 为了减小它的大小,我可以加快加载速度。 因为我的列表页面包含许多图像。所以我的加载时间很长。

所以你能告诉我如何压缩图像或获取缩略图吗???

【问题讨论】:

标签: c# asp.net image image-processing file-upload


【解决方案1】:

你可以这样做:

public static Bitmap CreateThumbnail(string filename, int width, int height)
{

    Bitmap bmpOut = null;
    try
    {
        Bitmap loBMP = new Bitmap(filename);
        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 < width && loBMP.Height < height)
            return loBMP;

        if (loBMP.Width > loBMP.Height)
        {
            lnRatio = (decimal)width / loBMP.Width;
            lnNewWidth = width;
            decimal lnTemp = loBMP.Height * lnRatio;
            lnNewHeight = (int)lnTemp;
        }

        else
        {
            lnRatio = (decimal)height / loBMP.Height;
            lnNewHeight = height;
            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;
}

只是一个原型,但您可以将其用于您的项目

【讨论】:

  • 不错的信息,但如果 (loBMP.Width > loBMP.Height)
猜你喜欢
  • 1970-01-01
  • 2020-03-15
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多