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

zek*_*kia 4 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; }
Run Code Online (Sandbox Code Playgroud)

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

Jer*_*ine 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;
}
Run Code Online (Sandbox Code Playgroud)

要与您的代码一起使用,您可以用以下代码替换您的呼叫OriginalImage.GetThumbnailImage:

imThumbnailImage = CreateThumbnail(OriginalImage, new Size(width, height));
Run Code Online (Sandbox Code Playgroud)

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


Fel*_*ano 7

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