VB icon

Basic Image Coverter Class

Email
Submitted on: 1/1/2015 5:25:00 AM
By: Chris Andersen (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB.NET
Views: 194
 
     This is a class that can turn any bitmap based image format into either, bmp, jpg, gif, tif, or png formats. It also includes setting the quality for Jpg type images. See the code in the module section for example usage. To compile this code, at the command line type vbc image.vb /r:system.drawing.dll . I will add more capabilities in the future. IMPORTASNT NOTE.. add an extra 's' to the words Clas. Sorry but I had to remove the s because PSC's really bad parsing for bad words saw the last three letters and wouldnt let me post it..Very bad form PSC!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Basic Image Coverter Class
// Description:This is a class that can turn any bitmap based image format into either, bmp, jpg, gif, tif, or png formats. It also includes setting the quality for Jpg type images. See the code in the module section for example usage. To compile this code, at the command line type vbc image.vb /r:system.drawing.dll . I will add more capabilities in the future.
IMPORTASNT NOTE.. add an extra 's' to the words Clas. Sorry but I had to remove the s because PSC's really bad parsing for bad words saw the last three letters and wouldnt let me post it..Very bad form PSC!
// By: Chris Andersen (from psc cd)
//**************************************

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Module App
	Sub Main()
		Dim tst As New ImageConverter
		
		tst.ImagePath = "c:\ufr1.bmp"
		tst.ImageType = tst.ImageType.Gif
		tst.Quality = 100
		tst.ConvertImage()
	End Sub
End Module
Public Clas ImageConverter
	Public Enum enumImageTypes
		Bitmap = 0
		Jpeg = 1
		Gif = 2
		Tiff = 3
		Png = 4
	End Enum
	
	Public ImagePath As String
	Public ImageType As enumImageTypes
	Public Quality As Integer
	
	Public Sub ConvertImage()
		Dim newBitmap As Bitmap = New Bitmap(ImagePath)
		Dim imgCodecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()		
		' Set quality Parameter for the Jpeg codec
		Dim imgParams As EncoderParameters = New EncoderParameters(1)
		Dim imgQuality As EncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality)
		Dim imgExt As String
		
		' Set quality
		imgParams.Param(0) = imgQuality
		
		'Get file extension of codec
		imgExt = imgCodecs(ImageType).FilenameExtension
		imgExt = imgext.SubString(1, imgExt.Length - 1)
		
		newBitmap.Save(ImagePath.SubString(0, ImagePath.Length - 4) + imgExt, imgCodecs(ImageType), imgParams)
		newBitmap.Dispose()
	End Sub
End Clas


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.