IT Community - Software Programming, Web Development and Technical Support

Image Compression. Save images with any quality..

This is a discussion on Image Compression. Save images with any quality.. within the VB.NET Programming forums, part of the Software Development category; JPEG Compression --> save JPEG images with any quality Using this code you can save an image (System.Drawing.Image) ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > VB.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-22-2008, 01:43 AM
Mramesh Mramesh is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Location: Chennai
Posts: 106
Mramesh is on a distinguished road
Send a message via MSN to Mramesh
Default Image Compression. Save images with any quality..

JPEG Compression --> save JPEG images with any quality

Using this code you can save an image (System.Drawing.Image) item as a jpeg image, specifying the quality of it. Jpeg quality is the same as compression so the lower the quality, the smaller the file size. It's simple to use, ie just do this:


Code:
Dim myImage As Image = ' ... load the image somehow
 ' Save the image with a quality of 50% 
SaveJpeg(destImagePath, myImage, 50)

Code:
'add this!
Imports System.Drawing.Imaging      
' Saves an image as a jpeg image, with the given quality     
' Gets:    
'   path   - Path to which the image would be saved.    
'   quality - An integer from 0 to 100, with 100 being the     
'           highest quality    
         Public Shared Sub SaveJpeg(ByVal path As String, ByVal img As Image, ByVal quality As Long)        
         If ((quality < 0) OrElse (quality > 100)) Then            
                  Throw New ArgumentOutOfRangeException("quality must be between 0 and 100.")        
         End If         
         ' Encoder parameter for image quality        
         Dim qualityParam As New EncoderParameter(Encoder.Quality, quality)        
         ' Jpeg image codec
         Dim jpegCodec As ImageCodecInfo = GetEncoderInfo("image/jpeg")

         Dim encoderParams As New EncoderParameters(1)
        encoderParams.Param(0) = qualityParam
        img.Save(path, jpegCodec, encoderParams)
    End Sub
       ' Returns the image codec with the given mime type
     Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
        ' Get image codecs for all image formats
         Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
         ' Find the correct image codec
         For i As Integer = 0 To codecs.Length - 1
            If (codecs(i).MimeType = mimeType) Then
                Return codecs(i)
            End If
        Next i
         Return Nothing
    End Function
__________________
M.Ramesh Kumar

Last edited by Mramesh : 02-22-2008 at 01:49 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Image Compression. Save images with any quality.. Mramesh PHP Programming 2 03-11-2008 12:16 AM
Image Compression. Save images with any quality.. Mramesh C# Programming 2 02-25-2008 06:33 AM
how do I save images to SQL DB? H2o ASP and ASP.NET Programming 2 09-18-2007 11:00 PM
Select Thumbnail images and view the larger image H2o HTML, CSS and Javascript Coding Techniques 1 08-07-2007 07:58 AM
Align images relative to a particular image? oyu2o HTML, CSS and Javascript Coding Techniques 0 03-10-2007 09:30 PM


All times are GMT -7. The time now is 09:30 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0