[英]Create thumbnail in .NET C# by defining width
我在我的網站中使用以下代碼來創建縮略圖:
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; }
我想更改此代碼,並且能夠創建僅定義寬度的縮略圖。 我想到的實際上是像裁剪/調整圖像大小,使用靜態寬度,保持它的比例。 那可能嗎;
你提到調整大小和裁剪。 如果您希望縮略圖高度隨固定寬度變化,那么已經提供的答案將適合您。
提到裁剪讓我覺得你可能想要一個固定的縮略圖大小,填充寬度並裁剪掉任何溢出的垂直部分。 如果是這種情況,您將需要做更多的工作。 我最近需要類似的東西,這就是我想出的。
這將創建一個原始縮略圖,其大小和裁剪方式使源圖像完全填充目標縮略圖,裁剪任何溢出。 即使原始和縮略圖的縱橫比不同,縮略圖內也不會有邊框。
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
塊。
讓我們讓 originalWidth = 原始圖像寬度和 thumbWidth。 您可以簡單地將 thumbWidth 選擇為您想要的值,然后計算thumbHeigth=originalHeigth*thumbWidth/originalWidth
我厭倦了需要這樣做並創建了一個可以輕松執行此操作的庫:鏈接到文檔和下載
一個沒有舍入且縮略圖寬度為 140 的基本示例,在“文件”下方是從 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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.