Last active
June 24, 2019 09:37
-
-
Save mshwf/fabb06483e02edbe97ce7a91f47602a6 to your computer and use it in GitHub Desktop.
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 | |
{ | |
static void Main(string[] args) | |
{ | |
var img = Image.FromFile(@"C:\Users\Mohamed.Elshawaf\Desktop\imageTests\original.png"); | |
var ratio = .75; | |
var height = img.Height; | |
var width = img.Width; | |
int newHeight = (int)(height * ratio); | |
int newWidth = (int)(width * ratio); | |
var bitmap = ResizeImage(img, newWidth, newHeight); | |
VaryQualityLevel(bitmap); | |
} | |
private static void VaryQualityLevel(Bitmap bitmap) | |
{ | |
// Get a bitmap. The using statement ensures objects | |
// are automatically disposed from memory after use. | |
using (bitmap) | |
{ | |
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); | |
// Create an Encoder object based on the GUID | |
// for the Quality parameter category. | |
System.Drawing.Imaging.Encoder myEncoder = | |
System.Drawing.Imaging.Encoder.Quality; | |
// Create an EncoderParameters object. | |
// An EncoderParameters object has an array of EncoderParameter | |
// objects. In this case, there is only one | |
// EncoderParameter object in the array. | |
EncoderParameters myEncoderParameters = new EncoderParameters(1); | |
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);//use this | |
myEncoderParameters.Param[0] = myEncoderParameter; | |
bitmap.Save(@"C:\Users\Mohamed.Elshawaf\Desktop\imageTests\originalQualityFifty.jpg", jpgEncoder, myEncoderParameters); | |
myEncoderParameter = new EncoderParameter(myEncoder, 100L); | |
myEncoderParameters.Param[0] = myEncoderParameter; | |
bitmap.Save(@"C:\Users\Mohamed.Elshawaf\Desktop\imageTests\originalQualityHundred.jpg", jpgEncoder, myEncoderParameters); | |
// Save the bitmap as a JPG file with zero quality level compression. | |
myEncoderParameter = new EncoderParameter(myEncoder, 0L); | |
myEncoderParameters.Param[0] = myEncoderParameter; | |
bitmap.Save(@"C:\Users\Mohamed.Elshawaf\Desktop\imageTests\originalQualityZero.jpg", jpgEncoder, myEncoderParameters); | |
} | |
} | |
private static ImageCodecInfo GetEncoder(ImageFormat format) | |
{ | |
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); | |
foreach (ImageCodecInfo codec in codecs) | |
{ | |
if (codec.FormatID == format.Guid) | |
{ | |
return codec; | |
} | |
} | |
return null; | |
} | |
/// <summary> | |
/// Resize the image to the specified width and height. | |
/// </summary> | |
/// <param name="image">The image to resize.</param> | |
/// <param name="width">The width to resize to.</param> | |
/// <param name="height">The height to resize to.</param> | |
/// <returns>The resized image.</returns> | |
public static Bitmap ResizeImage(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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment