Skip to content

Instantly share code, notes, and snippets.

@kiichi
Last active July 1, 2023 07:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kiichi/ba4b188d3f64ea5bc71b to your computer and use it in GitHub Desktop.
Save kiichi/ba4b188d3f64ea5bc71b to your computer and use it in GitHub Desktop.
Image Resize Example in C#
// Add System.Drawing as a reference.
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ImageResizeTest {
class Program {
static void Main(string[] args) {
string path = @"C:\\Users\kiichi\work\ImageResizeTest\geo-elevation.png";
Resize(path, 600, 600);
Resize(path, 300, 300);
Resize(path, 150, 150);
Resize(path, 225, 225);
Resize(path, 188, 188);
}
public static void Resize(string srcPath, int width, int height) {
Image image = Image.FromFile(srcPath);
Bitmap resultImage = Resize(image, width, height);
resultImage.Save(srcPath.Replace(".png", "_"+width+"x"+height+".png"));
}
//http://stackoverflow.com/questions/11137979/image-resizing-using-c-sharp
public static Bitmap Resize(Image image, int width, int height) {
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage)) {
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes()) {
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
}
}
// Resize image and if width and height are different ratio, keep it in center.
public static Bitmap Resize(Image image, int width, int height) {
int drawWidth = width;
int drawHeight = height;
if (width != height) {
drawWidth = Math.Min(width, height);
drawHeight = drawWidth;
}
var destRect = new Rectangle((width- drawWidth)/2, (height-drawHeight)/2, drawWidth, drawHeight);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage)) {
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes()) {
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment