NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
How Long For Me
SaveMySerials

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Resizing Images in C#

Wednesday, May 13th, 2009

Resizing images in C# is not that bad, but it does take some time to work out the specifics of maintaining image ratio, formatting percentages, and a few other quirks.

Without much further explanation, I present to you a very useful class for resizing images.

This class will also place the resized image on a canvas size of your choice, which allows for much better display of images. Of course it could be easily altered not to canvas the resized image, but you can handle that.

Here we go:

public enum ResizeModeEnum
{
Percentage,
Pixels
}

public class Resizer
{
private Image m_Image = null;
private ImageFormat m_Format = ImageFormat.Bmp;
private int m_OriginalWidth = 0;
private int m_OriginalHeight = 0;
private int m_NewWidth = 0;
private int m_NewHeight = 0;
private int m_Percentage = 0;
private bool m_MaintainAspectRatio = true;
private ResizeModeEnum m_ResizeMode = ResizeModeEnum.Pixels;

private void LoadData()
{
m_Format = m_Image.RawFormat;
m_OriginalWidth = m_Image.Width;
m_OriginalHeight = m_Image.Height;
}

public Resizer(string Path)
{
m_Image = Image.FromFile(Path);
LoadData();
}

public Resizer(System.IO.Stream Stream)
{
m_Image = Image.FromStream(Stream);
LoadData();
}

public Stream Resize(int CanvasWidth, int CanvasHeight)
{
int width = m_Image.Width;
int height = m_Image.Height;
if (m_MaintainAspectRatio)
{
if (m_ResizeMode == ResizeModeEnum.Pixels)
{
if (m_NewWidth != 0)
{
width = m_NewWidth;
height = Convert.ToInt32((m_NewWidth / (double)m_OriginalWidth) * m_OriginalHeight);
}
else
{
height = m_NewHeight;
width = Convert.ToInt32((m_NewHeight / (double)m_OriginalHeight) * m_OriginalWidth);
}
}
else
{

width = Convert.ToInt32(((double)m_Percentage / 100) * m_OriginalWidth);
height = Convert.ToInt32(((double)m_Percentage / 100) * m_OriginalHeight);
}
}
else
{
if (m_ResizeMode == ResizeModeEnum.Pixels)
{
width = m_NewWidth;
height = m_NewHeight;
}
else
{
width = Convert.ToInt32(((double)m_Percentage / 100) * m_OriginalWidth);
height = Convert.ToInt32(((double)m_Percentage / 100) * m_OriginalHeight);
}
}

Bitmap resizedImage = new Bitmap(m_Image, width, height);
Bitmap canvas = PlaceOnCanvas(resizedImage, CanvasWidth, CanvasHeight, true);
Stream toReturn = new MemoryStream();
canvas.Save(toReturn, m_Format);
m_Image.Dispose();
canvas.Dispose();
resizedImage.Dispose();
canvas.Dispose();
return toReturn;
}

private Bitmap PlaceOnCanvas(Bitmap input, int Width, int Height, bool Center)
{
float x = 0;
float y = 0;
if (Center) {
x = (Width / 2) - (input.Width / 2);
y = (Height / 2) - (input.Height / 2);
}
Bitmap canvas = new Bitmap(Width, Height);
Graphics drawer = Graphics.FromImage(canvas);
drawer.Clear(Color.White);
drawer.DrawImage(input, x, y, input.Width, input.Height);
return canvas;
}

public bool MaintainAspectRatio
{
get
{
return m_MaintainAspectRatio;
}
set
{
m_MaintainAspectRatio = value;
}
}

public int Width
{
get
{
return m_NewWidth;
}
set
{
m_NewWidth = value;
}
}

public int Height
{
get
{
return m_NewHeight;
}
set
{
m_NewHeight = value;
}
}

public ResizeModeEnum ResizeMode
{
get
{
return m_ResizeMode;
}
set
{
m_ResizeMode = value;
}
}

public int Percentage
{
get
{
return m_Percentage;
}
set
{
m_Percentage = value;
}
}
}

This class can be easily used to resize all of the images you need until your heart's content.

Resizin' Tom Out.

Tags

CSharp

Related Blogs

Retrieving the SID of a user or group account using the Win32 SDK and C#
Reading an XML file using LINQ
Creating an MD5 String Extension method in C#
ASP.NET Best Practices
Validate a Windows Username and Password against Active Directory

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers