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
|
| Author: James Fuhr 21 Oct 2004 | Member Level: Bronze Points : 0 |
Thanks for posting this code. Indeed it is easier with .NET!
|
| Author: Todd Merritt 19 Jun 2007 | Member Level: Bronze Points : 0 |
Great code, you just saved me hours of research.
|