To resize a image using C# or VB.NET you can use the following snippet.
Sample C#
public static Image ResizeImage(Image img, int width, int height) { var newImage = new Bitmap(width, height); using (var gr = Graphics.FromImage(newImage)) { gr.SmoothingMode = SmoothingMode.HighQuality; gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.PixelOffsetMode = PixelOffsetMode.HighQuality; gr.DrawImage(img, new Rectangle(0, 0, width, height)); } return newImage; } public static Image ResizeImage(Image img, Size size) { return ResizeImage(img, size.Width, size.Height); } public static Image ResizeImage(Bitmap bmp, int width, int height) { return ResizeImage((Image)bmp, width, height); } public static Image ResizeImage(Bitmap bmp, Size size) { return ResizeImage((Image)bmp, size.Width, size.Height); }
Sample VB.NET
Public Shared Function ResizeImage(img As Image, width As Integer, height As Integer) As Image Dim newImage = New Bitmap(width, height) Using gr = Graphics.FromImage(newImage) gr.SmoothingMode = SmoothingMode.HighQuality gr.InterpolationMode = InterpolationMode.HighQualityBicubic gr.PixelOffsetMode = PixelOffsetMode.HighQuality gr.DrawImage(img, New Rectangle(0, 0, width, height)) End Using Return newImage End Function Public Shared Function ResizeImage(img As Image, size As Size) As Image Return ResizeImage(img, size.Width, size.Height) End Function Public Shared Function ResizeImage(bmp As Bitmap, width As Integer, height As Integer) As Image Return ResizeImage(DirectCast(bmp, Image), width, height) End Function Public Shared Function ResizeImage(bmp As Bitmap, size As Size) As Image Return ResizeImage(DirectCast(bmp, Image), size.Width, size.Height) End Function
Here are the same methods as Extensions Methods
Sample C#
public static Image ResizeImage(this Image img, int width, int height) { var newImage = new Bitmap(width, height); using (var gr = Graphics.FromImage(newImage)) { gr.SmoothingMode = SmoothingMode.HighQuality; gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.PixelOffsetMode = PixelOffsetMode.HighQuality; gr.DrawImage(img, new Rectangle(0, 0, width, height)); } return newImage; } public static Image ResizeImage(this Image img, Size size) { return ResizeImage(img, size.Width, size.Height); } public static Image ResizeImage(this Bitmap bmp, int width, int height) { return ResizeImage((Image)bmp, width, height); } public static Image ResizeImage(this Bitmap bmp, Size size) { return ResizeImage((Image)bmp, size.Width, size.Height); }
Sample VB.NET
<System.Runtime.CompilerServices.Extension> _ Public Shared Function ResizeImage(img As Image, width As Integer, height As Integer) As Image Dim newImage = New Bitmap(width, height) Using gr = Graphics.FromImage(newImage) gr.SmoothingMode = SmoothingMode.HighQuality gr.InterpolationMode = InterpolationMode.HighQualityBicubic gr.PixelOffsetMode = PixelOffsetMode.HighQuality gr.DrawImage(img, New Rectangle(0, 0, width, height)) End Using Return newImage End Function <System.Runtime.CompilerServices.Extension> _ Public Shared Function ResizeImage(img As Image, size As Size) As Image Return ResizeImage(img, size.Width, size.Height) End Function <System.Runtime.CompilerServices.Extension> _ Public Shared Function ResizeImage(bmp As Bitmap, width As Integer, height As Integer) As Image Return ResizeImage(DirectCast(bmp, Image), width, height) End Function <System.Runtime.CompilerServices.Extension> _ Public Shared Function ResizeImage(bmp As Bitmap, size As Size) As Image Return ResizeImage(DirectCast(bmp, Image), size.Width, size.Height) End Function
RT @CodeSnippetsNET: How to resize a image using C# or http://t.co/85naMTtT4u: http://t.co/g8Jj6WRS7j #csharp #vb #dotnet #winforms #progra…