The code creates source image object from an image file. It also creates empty destination bitmap object. Note that the Bitmap class is subclassed from the Image class. GDI+ flat API calls allow to use bitmap handles as if they were image handles.
Then the code uses the graphics of the destination bitmap both as a tool and canvas to draw the scaled source image on it. Finally the destination bitmap saves itself to file.
The code is based on custom GDI+ class. Download the class module first and save it in gdiplus.prg file.
Make sure the source and destination file names are valid. The source file can be in any of graphics formats supported by the GDI+: BMP, GIF, JPG, TIF, EMF... The destination file type is limited to BMP, JPG, GIF, PNG and TIFF.
See also:
GDI+: cropping images
GDI+: rotating images
|
Imports System
Imports System.IO
Imports System.Drawing
Public Class ImageResizer
Public errorno As Integer = 0
Public errormessage As String = ""
Protected _sourcefilename As String = ""
Protected _sourceimage As System.Drawing.Image
Protected _resizedimage As System.Drawing.Image
Public Property sourcefilename() As String
Get
Return _sourcefilename
End Get
Set(ByVal Value As String)
ClearError()
If Not _sourceimage Is Nothing Then
_sourceimage.Dispose()
_sourceimage = Nothing
End If
Try
_sourceimage = Image.FromFile(Value)
_sourcefilename = Value
Catch e As Exception
_sourcefilename = ""
SetError(-1, e.Message)
End Try
End Set
End Property
Public ReadOnly Property sourceimage() As Image
Get
Return _sourceimage
End Get
End Property
Public Sub New(Optional ByVal filename As String = "")
If filename.Length > 0 Then sourcefilename = filename
End Sub
Protected Sub ClearError()
SetError(0, "")
End Sub
Protected Sub SetError(ByVal n As Integer, _
ByVal msg As String)
errorno = n
errormessage = msg
End Sub
Public Sub SaveResizedimage(ByVal filename As String)
If _resizedimage Is Nothing Then
SetError(-1, "The image has not been resized yet.")
Return
End If
_resizedimage.Save(filename, Imaging.ImageFormat.Jpeg)
End Sub
Protected Function ThumbnailCallback() As Boolean
SetError(-1, "CreateThumbnail function failed.")
Return False
End Function
Public Sub ResizeImage(ByVal width As Integer, _
ByVal height As Integer, Optional ByVal mode As Integer = 0)
If _sourceimage Is Nothing Then
SetError(-1, "The source image is not set.")
Return
End If
If Not _resizedimage Is Nothing Then
_resizedimage.Dispose()
_resizedimage = Nothing
End If
Dim xscale As Single = _sourceimage.Width / width
Dim yscale As Single = _sourceimage.Height / height
Dim scale As Single = IIf(xscale > yscale, xscale, yscale)
If scale < 1.0 Then scale = 1.0
width = CInt(_sourceimage.Width / scale)
height = CInt(_sourceimage.Height / scale)
Select Case mode
Case 0
Try
_resizedimage = New Bitmap(width, height)
Dim g As Graphics = _
Graphics.FromImage(_resizedimage)
g.DrawImage(_sourceimage, 0, 0, _
_resizedimage.Width + 1, _
_resizedimage.Height + 1)
Catch e As Exception
SetError(-1, e.Message)
End Try
Case 1
Try
_resizedimage = _sourceimage.GetThumbnailImage( _
width, height, Nothing, IntPtr.Zero)
Catch e As Exception
SetError(-1, e.Message)
End Try
End Select
End Sub
End Class
|