C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Resizing Images Dynamically


Posted Date: 05 Aug 2004    Resource Type: Articles    Category: .NET Framework
Author: Muralidharan RamakrishnanMember Level: Gold    
Rating: 1 out of 5Points: 10



Introduction

One of the harder things with VB6 was image manipulation. The Graphics object's DrawImage method copies an image much as Visual Basic 6's PaintPicture method does. In .NET, however, there are 30 overloaded versions of this routine. One of them takes as parameters the source bitmap and destination position for the copied image.

This sub takes a source bitmap and makes a new Bitmap object of the desired final size. It then uses that object's DrawImage routine to copy the source image into the entire destination image. It then saves the resulting image to the destination path.


Public Sub ResizeAndSaveImage(ByVal ImagePath As String, _
ByVal NewWidth As Integer, _
ByVal NewHeight As Integer, _
ByVal DestinationPath As String)

'Load the source bitmap
Dim bm_source As New Bitmap(ImagePath)

'Create the destination bitmap
Dim bm_dest As New Bitmap(NewWidth, NewHeight)

'Create a graphics object
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

'Re-draw the image to the specified height and width
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width , bm_dest.Height )

'Save the resulting image
bm_dest.Save(DestinationPath)

'Release objects
bm_source.Dispose()
bm_source = Nothing
bm_source = Nothing
bm_dest = Nothing
gr_dest = Nothing

End Sub




Responses

Author: James Fuhr    21 Oct 2004Member Level: Bronze   Points : 0
Thanks for posting this code. Indeed it is easier with .NET!


Author: Todd Merritt    19 Jun 2007Member Level: Bronze   Points : 0
Great code, you just saved me hours of research.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to load Assemblies dynamically using Reflection
Previous Resource: Using Windows Fax Service of Windows 2000 in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use