How To Resize A Image Using C# Or VB.NET - CODE SNIPPETS

Skip to content .NET C# VB.Net How to resize a image using C# or VB.NET

ByAdministrator

Jun 2, 2014 .net, bitmap, C#, csharp, Drawing, image, resize, System, System.Drawing, vb.net, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio 2012, Visual Studio 2013

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

Post navigation

How to change Checkstate of an item in a checkedlistbox control in C# or VB.NET How to use the DebuggerDisplay Attribut

By Administrator

Related Post

.NET C# Devexpress VB.Net

How to export Gridview data to Excel using devexpress XtraGrid

Nov 28, 2015 Administrator .NET C# VB.Net

How to catch specific MS-SQL SQLExceptions in C# and VB.NET

Nov 20, 2015 Administrator .NET C# VB.Net

How to extract a password protected zip file using DotNetZip in C# and VB.NET

Nov 12, 2015 Administrator
One thought on “How to resize a image using C# or VB.NET”
  1. RT @CodeSnippetsNET: How to resize a image using C# or http://t.co/85naMTtT4u: http://t.co/g8Jj6WRS7j #csharp #vb #dotnet #winforms #progra…

    Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Search
Categories
  • .NET
  • Android
  • AngularJs
  • Announcements
  • Batch
  • C
  • C#
  • C-PlusPlus
  • CSS
  • Devexpress
  • HTML / HTTP / WEB
  • ios
  • Java
  • Javascript
  • jQuery
  • Microsoft SQL Server
  • Mobile
  • Most Read Articles
  • MySql
  • NuGet
  • Objective-C
  • Oracle
  • PHP
  • Powershell
  • Python
  • Regex
  • Ruby
  • SQLite
  • VB.Net
  • VB6
  • VBA
  • VBScript
  • Wordpress
  • XML
  • XSD
  • XSL
Meta
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Archives
  • August 2016
  • June 2016
  • December 2015
  • November 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • November 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkPrivacy policy

Từ khóa » Visual Basic Stretch Image