What's Here?
- Members: 137,225
- Replies: 481,484
- Topics: 75,058
- Snippets: 2,567
- Tutorials: 675
- Total Online: 2,088
- Members: 106
- Guests: 1,982
|
This is a snippet for resizing an image based on a percentage. If the user wants the image to be 75% of the original then pass the number 75
|
Submitted By: PsychoCoder
|
|
|
Rating:
|
|
Views: 2,548 |
Language: C#
|
|
Last Modified: May 25, 2008 |
|
Instructions: Need references to System.Drawing, System.Drawing.Drawing2D, System.Drawing.Imaging Namespaces |
Snippet
//Namespace Reference
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/// <summary>
/// method for resizing an image
/// </summary>
/// <param name="img">the image to resize</param>
/// <param name="percentage">Percentage of change (i.e for 105% of the original provide 105)</param>
/// <returns></returns>
public Image Resize(Image img, int percentage)
{
//get the height and width of the image
int originalW = img.Width;
int originalH = img.Height;
//get the new size based on the percentage change
int resizedW = (int)(originalW * percentage);
int resizedH = (int)(originalH * percentage);
//create a new Bitmap the size of the new image
Bitmap bmp = new Bitmap (resizedW, resizedH );
//create a new graphic from the Bitmap
Graphics graphic = Graphics.FromImage((Image)bmp);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
//draw the newly resized image
graphic.DrawImage(img, 0, 0, resizedW, resizedH);
//dispose and free up the resources
graphic.Dispose();
//return the image
return (Image)bmp;
}
Copy & Paste
|
|
|
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|