M
Michael
I'm trying to use the Bitmap class's Save Method to save a Bitmap as a
GIF. My code runs fine, and converts the image, but the quality is
not anywhere close to as good. I know that GIF quality is not a good
as a BMP's but, is there a way to convert the image while preserving
its quality. Any help would be greatly appreciated. Here's what I
have...
Public Enum ImageType
Bmp = 0
Emf = 1
Exif = 2
Gif = 3
Icon = 4
Jpeg = 5
MemoryBmp = 6
Png = 7
Tiff = 8
Wmf = 9
End Enum
Public Sub ConvertImage(ByVal ConvertImageTo As ImageType, _
ByVal SourceImagePath As String,
Optional ByVal DeleteSouceFile As Boolean
= True)
Dim oImage As Bitmap
Try
'Make sure the source file even exists before we start
If Not File.Exists(SourceImagePath) Then
Err.Raise(10001, "ImagingUtility.ConvertImage", "The
image '" & SourceImagePath & "' does not
exist.")
Exit Sub
End If
oImage = New Bitmap(SourceImagePath)
Select Case ConvertImageTo
Case ImageType.Bmp :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".bmp",
Imaging.ImageFormat.Bmp)
Case ImageType.Emf
Case ImageType.Exif
Case ImageType.Gif :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".gif",
Imaging.ImageFormat.Gif)
Case ImageType.Icon :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".ico",
Imaging.ImageFormat.Icon)
Case ImageType.Jpeg :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".jpeg",
Imaging.ImageFormat.Jpeg)
Case ImageType.MemoryBmp
Case ImageType.Png :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".png",
Imaging.ImageFormat.Png)
Case ImageType.Tiff :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".tiff",
Imaging.ImageFormat.Tiff)
Case ImageType.Wmf
End Select
oImage.Dispose()
oImage = Nothing
'Delete the sourcefile if necessary
If DeleteSouceFile Then File.Delete(SourceImagePath)
Catch ex As Exception
Err.Raise(Err.Number, "ImageUtility.ConvertImage",
ex.Message)
End Try
End Sub
GIF. My code runs fine, and converts the image, but the quality is
not anywhere close to as good. I know that GIF quality is not a good
as a BMP's but, is there a way to convert the image while preserving
its quality. Any help would be greatly appreciated. Here's what I
have...
Public Enum ImageType
Bmp = 0
Emf = 1
Exif = 2
Gif = 3
Icon = 4
Jpeg = 5
MemoryBmp = 6
Png = 7
Tiff = 8
Wmf = 9
End Enum
Public Sub ConvertImage(ByVal ConvertImageTo As ImageType, _
ByVal SourceImagePath As String,
Optional ByVal DeleteSouceFile As Boolean
= True)
Dim oImage As Bitmap
Try
'Make sure the source file even exists before we start
If Not File.Exists(SourceImagePath) Then
Err.Raise(10001, "ImagingUtility.ConvertImage", "The
image '" & SourceImagePath & "' does not
exist.")
Exit Sub
End If
oImage = New Bitmap(SourceImagePath)
Select Case ConvertImageTo
Case ImageType.Bmp :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".bmp",
Imaging.ImageFormat.Bmp)
Case ImageType.Emf
Case ImageType.Exif
Case ImageType.Gif :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".gif",
Imaging.ImageFormat.Gif)
Case ImageType.Icon :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".ico",
Imaging.ImageFormat.Icon)
Case ImageType.Jpeg :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".jpeg",
Imaging.ImageFormat.Jpeg)
Case ImageType.MemoryBmp
Case ImageType.Png :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".png",
Imaging.ImageFormat.Png)
Case ImageType.Tiff :
oImage.Save(Path.GetDirectoryName(SourceImagePath) & "\" &
Path.GetFileNameWithoutExtension(SourceImagePath) & ".tiff",
Imaging.ImageFormat.Tiff)
Case ImageType.Wmf
End Select
oImage.Dispose()
oImage = Nothing
'Delete the sourcefile if necessary
If DeleteSouceFile Then File.Delete(SourceImagePath)
Catch ex As Exception
Err.Raise(Err.Number, "ImageUtility.ConvertImage",
ex.Message)
End Try
End Sub