webForumDet fria alternativet

.NET - Bicubic image resize

Programmering

0 svar · 4 115 visningar · startad av toha

Medlem sedan dec. 1999707 inlägg
Frågan#1
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

		public static void GenerateThumbNail(Bitmap bitmap, ImageFormat imageFormat, int width, int height, string destinationPath, bool framed)
		{
			// generate a white empty area.
			if (framed)
			{
				using (System.Drawing.Image oThumbNail =	new Bitmap(bitmap, width, height))
				{
					Graphics oGraphic =				Graphics.FromImage(oThumbNail);
					oGraphic.CompositingQuality =	CompositingQuality.HighQuality ;
					oGraphic.SmoothingMode =		SmoothingMode.HighQuality ;
					oGraphic.InterpolationMode =	InterpolationMode.HighQualityBicubic ;
					oGraphic.FillRectangle(new SolidBrush(Color.White), -2, -2, oThumbNail.Size.Width+2, oThumbNail.Size.Height+2);
					
					Size newSize =	GetCalculatedSize(bitmap.Size, width, height);
					int x =			(newSize.Width < oThumbNail.Size.Width) ? ((oThumbNail.Size.Width - newSize.Width) / 2) : 0;
					int y =			(newSize.Height < oThumbNail.Size.Height) ? ((oThumbNail.Size.Height - newSize.Height) / 2) : 0;

					Rectangle oRectangle =			new Rectangle(x, y, newSize.Width, newSize.Height);
					oGraphic.DrawImage(bitmap, oRectangle);
					oThumbNail.Save(destinationPath, imageFormat);
				}
			}
			else
			{
				Size newSize =	GetCalculatedSize(bitmap.Size, width, height);
				using (System.Drawing.Image oThumbNail =	new Bitmap(bitmap, newSize.Width, newSize.Height))
				{
					Graphics oGraphic =				Graphics.FromImage(oThumbNail);
					oGraphic.CompositingQuality =	CompositingQuality.HighQuality ;
					oGraphic.SmoothingMode =		SmoothingMode.HighQuality ;
					oGraphic.InterpolationMode =	InterpolationMode.HighQualityBicubic ;
					
					Rectangle oRectangle =			new Rectangle(0, 0, newSize.Width, newSize.Height);
					oGraphic.DrawImage(bitmap, oRectangle);
					oThumbNail.Save(destinationPath, imageFormat);
				}
			}
		}

		public static void ResizeImage(Bitmap bitmap, ImageFormat imageFormat, int width, int height, string destinationPath)
		{
			// generate a white empty area.
			using (System.Drawing.Image oThumbNail =	new Bitmap(bitmap, width, height))
			{
				Graphics oGraphic =				Graphics.FromImage(oThumbNail);
				oGraphic.CompositingQuality =	CompositingQuality.HighQuality ;
				oGraphic.SmoothingMode =		SmoothingMode.HighQuality ;
				oGraphic.InterpolationMode =	InterpolationMode.HighQualityBicubic ;
				oGraphic.FillRectangle(new SolidBrush(Color.White), -2, -2, oThumbNail.Size.Width+2, oThumbNail.Size.Height+2);
				
				Size newSize =	GetCalculatedSize(bitmap.Size, width, height);
				int x =			(newSize.Width < oThumbNail.Size.Width) ? ((oThumbNail.Size.Width - newSize.Width) / 2) : 0;
				int y =			(newSize.Height < oThumbNail.Size.Height) ? ((oThumbNail.Size.Height - newSize.Height) / 2) : 0;

				Rectangle oRectangle =			new Rectangle(x, y, newSize.Width, newSize.Height);
				oGraphic.DrawImage(bitmap, oRectangle);
				oThumbNail.Save(destinationPath, imageFormat);
			}
		}

		public static Size GetCalculatedSize(Size originalSize, int maxWidth, int maxHeight)
		{
			Size newSize =	new Size(1,1);
			float thumbProps =		(float)maxWidth / (float)maxHeight;
			float imgProps =		(float)originalSize.Width / (float)originalSize.Height;
			if (imgProps > thumbProps)
			{
				// width is max
				newSize.Width =		maxWidth;
				newSize.Height =	System.Convert.ToInt32(maxWidth / imgProps);
			}
			else
			{
				// height is max
				newSize.Height =	maxHeight;
				newSize.Width =		System.Convert.ToInt32(maxHeight * imgProps);
			}
			return newSize;
		}
264 ms totalt · 4 externa anrop · v20260731065814-full.a51de22e
127 ms — deklarationer (db)
0 ms — hämta statistik (cache)
133 ms — hämta tråd, inlägg och bilagor (db)
129 ms — ändringar (db)