---
title: ".NET - Bicubic image resize"
type: "forum-thread"
url: "https://www.webforum.nu/amne/programmering/119552-net-bicubic-image-resize"
topic: "Programmering"
topic_url: "https://www.webforum.nu/amne/programmering"
author: "toha"
published: "2005-01-07T13:31:02.000Z"
updated: "2005-01-07T13:31:02.000Z"
replies: 0
views: 4137
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/programmering/119552-net-bicubic-image-resize"
---

# .NET - Bicubic image resize

## #1 — toha, 2005-01-07T13:31Z

```
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;
		}
```

Permalänk: https://www.webforum.nu/p/119552

---

Tråden på webben: https://www.webforum.nu/amne/programmering/119552-net-bicubic-image-resize
