Important alert: (current site time 11/28/2014 10:12:14 AM EDT)
 

VB icon

Basic Image Coverter Class

Email
Submitted on: 6/11/2002 11:26:05 AM
By: Chris Andersen 
Level: Beginner
User Rating: By 6 Users
Compatibility: VB.NET
Views: 16825
(About the author)
 
     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!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// 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
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=325&lngWId=10//for details.//**************************************

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 13 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

7/11/2002 8:19:05 AMAcid Flux

Nice code man!!! 5 globes from me
(If this comment was disrespectful, please report it.)

 
8/6/2002 11:24:12 AMRobert Smith

Don't suppose you could figure out how to save a tif as a black
(If this comment was disrespectful, please report it.)

 
8/6/2002 11:26:04 AMRobert Smith

oy, psc chopped my comment, here's another try:

Don't suppose you could figure out how
to save a tif as a black and white CCIT4, could you? No One (at all) seems to know how to do that ... and asking the MS experts on the newsgroups is a great way to get them to become very very quiet :) )

-s
(If this comment was disrespectful, please report it.)

 
8/17/2002 6:23:19 PMHidri Mohamed

Excelent, But Can U tell me wher can i found a free Vb.net Books for Download.
And How Have u learned the vb.net
(If this comment was disrespectful, please report it.)

 
8/22/2002 11:20:05 PMDaniel Sitnik

Excelent!
(If this comment was disrespectful, please report it.)

 
3/12/2003 12:18:59 AM

Great Code!!! Thank you!!!
(If this comment was disrespectful, please report it.)

 
5/31/2003 9:21:13 AMGovind

Good Work
(If this comment was disrespectful, please report it.)

 
7/7/2003 2:00:49 PMCodeFester

The code worked as stated for bitmaps to GIFs but needed some tinkering to accomodate JPEGS due to varying file extensions and the means being used to assign such. Overall though, I am thrilled to find native support for image conversion in .NET It converted easily to C#. 5 balls for saving my day.
(If this comment was disrespectful, please report it.)

 
8/30/2003 1:19:08 AMDanny J

This code was copied from a website I saw before, you code stealer!
(If this comment was disrespectful, please report it.)

 
9/7/2003 11:59:34 PMPsyc

Hi, nice code you have... but could you try to include a compiled form of your code for people who lack the .NET compilers? Thanks alot!
(If this comment was disrespectful, please report it.)

 
9/18/2003 12:40:11 AMDanny J

Poor, poor coding. You don't have to use the image converter class. Soonly I will post an image converter that will go with several more image formats than this...
(If this comment was disrespectful, please report it.)

 
1/1/2005 6:09:15 PMPio

Fairly good, you should have supported compression for tiff's aswell.
(If this comment was disrespectful, please report it.)

 

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.