Last active
December 18, 2015 07:20
-
-
Save ArjunVachhani/b093855e71dc377e1d90 to your computer and use it in GitHub Desktop.
image file thumb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
public static void GenerateImageFileThumb(string file, string outputFile, float width, float height, ThumbOption option) | |
{ | |
if (option == ThumbOption.Scale) | |
{ | |
using (Bitmap image = new Bitmap(file)) | |
{ | |
float scale = Math.Min(width / image.Width, height / image.Height); | |
var scaleWidth = (int)(image.Width * scale); | |
var scaleHeight = (int)(image.Height * scale); | |
var bmp = new Bitmap(scaleWidth, scaleHeight); | |
var graph = Graphics.FromImage(bmp); | |
var brush = new SolidBrush(Color.Transparent); | |
graph.FillRectangle(brush, new RectangleF(0, 0, scaleWidth, scaleHeight)); | |
graph.DrawImage(image, new Rectangle(0, 0, scaleWidth, scaleHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); | |
bmp.Save(outputFile); | |
} | |
} | |
else if (option == ThumbOption.FillTransparency) | |
{ | |
using (Bitmap image = new Bitmap(file)) | |
{ | |
float scale = Math.Min(width / image.Width, height / image.Height); | |
var scaleWidth = (int)(image.Width * scale); | |
var scaleHeight = (int)(image.Height * scale); | |
var bmp = new Bitmap((int)width, (int)height); | |
var graph = Graphics.FromImage(bmp); | |
var brush = new SolidBrush(Color.Transparent); | |
graph.FillRectangle(brush, new RectangleF(0, 0, width, height)); | |
var startX = (int)(Math.Abs(width - scaleWidth) / 2); | |
var startY = (int)(Math.Abs(height - scaleHeight) / 2); | |
graph.DrawImage(image, new Rectangle(startX, startY, scaleWidth, scaleHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); | |
bmp.Save(outputFile); | |
} | |
} | |
else | |
{ | |
using (Bitmap image = new Bitmap(file)) | |
{ | |
float scale = Math.Min(image.Width / width, image.Height / height); | |
var scaleWidth = (int)(image.Width / scale); | |
var scaleHeight = (int)(image.Height / scale); | |
var bmp = new Bitmap((int)width, (int)height); | |
var graph = Graphics.FromImage(bmp); | |
var brush = new SolidBrush(Color.Transparent); | |
graph.FillRectangle(brush, new RectangleF(0, 0, width, height)); | |
var startX = (int)((width - scaleWidth) / 2); | |
var startY = (int)((height - scaleHeight) / 2); | |
graph.DrawImage(image, new Rectangle(startX, startY, scaleWidth, scaleHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); | |
bmp.Save(outputFile); | |
} | |
} | |
} | |
} | |
public enum ThumbOption | |
{ | |
Scale, | |
FillTransparency, | |
Cover | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment