NerdyHearn
Home

IPhone SMS To Gmail

Consulting

Blog
Software
Projects
Links
About
Contact

Blog
Facebook
Twitter
LinkedIn

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

Bitmasking userAccountControl attribute in LDAP from C#

ConnectionString Switcharoo
Good Ol Cross-Threaded Socket Action
Code Analysis with NDepend V3

Comments

Currently no comments.

Add A Comment

Name:


URL:


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


Comments:


Please enter the text from the image:



NerdyHearn - Latest tech news relating to C#, SharePoint, PHP, general development, and more. LiveNation Feed - Feeds of all your local artists, concerts, and events through your favorite RSS reader SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers Tweets2Mail - Backup and search your twitter stream from Gmail TimeSinceI - Track how long you have quit smoking, how long you have been married, or how long you have done anything.