通过定义宽度在.NET C#中创建缩略图

时间:2011-01-11 14:02:58

标签: c# resize thumbnails

我在我的网站上使用以下代码创建缩略图:

string furl = "~/images/thumbs/" + matchString;
lBlogThumb.ImageUrl = GetThumbnailView(furl, 200, 200);


private string GetThumbnailView(string originalImagePath, int height, int width)
        {
            //Consider Image is stored at path like "ProductImage\\Product1.jpg"

            //Now we have created one another folder ProductThumbnail to store thumbnail image of product.
            //So let name of image be same, just change the FolderName while storing image.
            string thumbnailImagePath = originalImagePath.Replace("thumbs", "thumbs2");
            //If thumbnail Image is not available, generate it.
            if (!System.IO.File.Exists(Server.MapPath(thumbnailImagePath)))
            {
                System.Drawing.Image imThumbnailImage;
                System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Server.MapPath(originalImagePath));
                imThumbnailImage = OriginalImage.GetThumbnailImage(width, height,
                             new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

                imThumbnailImage.Save(Server.MapPath(thumbnailImagePath), System.Drawing.Imaging.ImageFormat.Jpeg);

                imThumbnailImage.Dispose();
                OriginalImage.Dispose();
            }
            return thumbnailImagePath;
        }

public bool ThumbnailCallback() { return false; }

我想更改此代码,并且只能创建定义宽度的缩略图。我想到的实际上是像裁剪/调整图像大小,使用静态宽度,保持它的比例。这可能吗;

4 个答案:

答案 0 :(得分:15)

你提到调整大小和裁剪。如果您希望缩略图高度以固定宽度变化,则已提供的答案已经适合您。

提到裁剪让我觉得你可能想要一个固定的缩略图大小,宽度已填满,任何溢出的垂直部分都会被裁掉。如果是这种情况,您需要做更多的工作。我最近需要类似的东西,这就是我想出来的。

这将创建原始缩略图,其大小和裁剪方式使源图像完全填充目标缩略图,裁剪任何溢出。即使原始和缩略图宽高比不同,缩略图中也不会有边框。

public System.Drawing.Image CreateThumbnail(System.Drawing.Image image, Size thumbnailSize)
{
    float scalingRatio = CalculateScalingRatio(image.Size, thumbnailSize);

    int scaledWidth = (int)Math.Round((float)image.Size.Width * scalingRatio);
    int scaledHeight = (int)Math.Round((float)image.Size.Height * scalingRatio);
    int scaledLeft = (thumbnailSize.Width - scaledWidth) / 2;
    int scaledTop = (thumbnailSize.Height - scaledHeight) / 2;

    // For portrait mode, adjust the vertical top of the crop area so that we get more of the top area
    if (scaledWidth < scaledHeight && scaledHeight > thumbnailSize.Height)
    {
        scaledTop = (thumbnailSize.Height - scaledHeight) / 4;
    }

    Rectangle cropArea = new Rectangle(scaledLeft, scaledTop, scaledWidth, scaledHeight);

    System.Drawing.Image thumbnail = new Bitmap(thumbnailSize.Width, thumbnailSize.Height);
    using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnail))
    {
        thumbnailGraphics.CompositingQuality = CompositingQuality.HighQuality;
        thumbnailGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        thumbnailGraphics.SmoothingMode = SmoothingMode.HighQuality;
        thumbnailGraphics.DrawImage(image, cropArea);
    }
    return thumbnail;
}

private float CalculateScalingRatio(Size originalSize, Size targetSize)
{
    float originalAspectRatio = (float)originalSize.Width / (float)originalSize.Height;
    float targetAspectRatio = (float)targetSize.Width / (float)targetSize.Height;

    float scalingRatio = 0;

    if (targetAspectRatio >= originalAspectRatio)
    {
        scalingRatio = (float)targetSize.Width / (float)originalSize.Width;
    }
    else
    {
        scalingRatio = (float)targetSize.Height / (float)originalSize.Height;
    }

    return scalingRatio;
}

要与您的代码一起使用,您可以将此OriginalImage.GetThumbnailImage的来电替换为:

imThumbnailImage = CreateThumbnail(OriginalImage, new Size(width, height));

请注意,对于纵向图像,此代码实际上会将缩略图的视口移动到原始图像上稍高的位置。这样做是为了在创建缩略图时人们的肖像照片不会产生无头躯干。如果您不想要这种逻辑,只需删除“纵向模式”注释后的if块。

答案 1 :(得分:7)

让我们有originalWidth =原始图像宽度和thumbWidth。您只需选择thumbWidth到您想要的值,然后计算thumbHeigth=originalHeigth*thumbWidth/originalWidth

答案 2 :(得分:1)

我厌倦了这样做并创建了一个可轻松完成此操作的库:Link To Documentation & Download

答案 3 :(得分:0)

没有舍入宽度为140的基本示例,在'file'下面是从ASP.Net FileUpload控件上传的HttpPostedFile,HttpPostedFile公开了一个流。

// Save images to disk.
using (System.Drawing.Image image = System.Drawing.Image.FromStream(file.InputStream))
using (System.Drawing.Image thumbnailImage = image.GetThumbnailImage(140, Convert.ToInt32((image.Height / (image.Width / 140))), null, IntPtr.Zero))
{
    if (image != null)
    {
        image.Save(imageFilePath);
        thumbnailImage.Save(thumbnailImagePath); 
    }
    else
        throw new ArgumentNullException("Image stream is null");
}