Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.19/5 (4 votes)
See more:
i need a vb.net source code for image compression
Posted

Then Google for it. Were not going to do your research not quite your code for you. By the way, there's a good reason why you don't see a lot of VB.NET code for image manipulation on the web.
 
Share this answer
 
Save an image as a .jpg and specify compression ratio:

VB
Private Class JpegTools
     Private codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
     Private quality As Long

     Public ici As ImageCodecInfo = Nothing
     Public ep As New EncoderParameters()
     Public compressionRatio As Long

     Public Sub new(ByVal _compressionRatio As Long, Optional ByRef errMsg As String = "")

         compressionRatio    = _compressionRatio
         If compressionRatio < 0 then compressionRatio = 0
         If compressionRatio > 100 then compressionRatio = 100
         quality             = (100 - compressionRatio)

         Try
             For Each codec As ImageCodecInfo In codecs
                 If codec.MimeType = "image/jpeg" Then
                     ici = codec
                 End If
             Next

             ep.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality)
         Catch ex As Exception
             errMsg = ex.Message
         End Try
     End Sub
 End Class

 Private JpgTools As JpegTools

 ' Save an Image() to a jpeg file and specify the compression % (Valid values for compressionRatio are 0 - 100)
 Public Function SaveImgToFile(ByRef img As Image, ByVal fullPathWithFileName As String, ByVal compressionRatio As Long, _
                               Optional ByRef errMsg As String = "") As Boolean

     If JpgTools Is Nothing Then JpgTools = New JpegTools(compressionRatio, errMsg)
     If JpgTools.compressionRatio <> compressionRatio then JpgTools = New JpegTools(compressionRatio, errMsg)
     If errMsg <> "" then Return False

     Try
         img.Save(fullPathWithFileName, JpgTools.ici, JpgTools.ep)
     Catch ex As Exception
         errMsg = ex.Message
         Return False
     End Try

     Return True
 End Function
 
Share this answer
 
v2
Comments
Dave Kreskowiak 21-May-13 16:37pm    
... and if the OP isn't using JPEG?? Then what?

We don't know because he never specified what his requirements were.
pdoxtader 21-May-13 17:15pm    
Well, then I guess he'd better start using jpeg. Lol... Your right. He didn't specify... I don't think he cares. I think he's just getting a handle on image compression, and this its not a bad place to start.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900