PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
save JPEG images with any quality-VBForums
Results 1 to 5 of 5

Thread: save JPEG images with any quality

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    save JPEG images with any quality

    (C# version of this article is here )

    Here's the code! Too many people ask for this
    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:
    VB Code:
    1. Dim myImage As Image = ' ... load the image somehow
    2. ' Save the image with a quality of 50%
    3. SaveJpeg(destImagePath, myImage, 50)

    VB Code:
    1. ' Please do not remove :)
    2.     ' Written by Kourosh Derakshan
    3.     '
    4.  
    5.     'add this!
    6.     Imports System.Drawing.Imaging
    7.  
    8.  
    9.     ' Saves an image as a jpeg image, with the given quality
    10.     ' Gets:
    11.     '   path    - Path to which the image would be saved.
    12.     '   quality - An integer from 0 to 100, with 100 being the
    13.     '             highest quality
    14.     Public Shared Sub SaveJpeg(ByVal path As String, ByVal img As Image, ByVal quality As Long)
    15.         If ((quality < 0) OrElse (quality > 100)) Then
    16.             Throw New ArgumentOutOfRangeException("quality must be between 0 and 100.")
    17.         End If
    18.  
    19.         ' Encoder parameter for image quality
    20.         Dim qualityParam As New EncoderParameter(Encoder.Quality, quality)
    21.         ' Jpeg image codec
    22.         Dim jpegCodec As ImageCodecInfo = GetEncoderInfo("image/jpeg")
    23.  
    24.         Dim encoderParams As New EncoderParameters(1)
    25.         encoderParams.Param(0) = qualityParam
    26.         img.Save(path, jpegCodec, encoderParams)
    27.     End Sub
    28.  
    29.  
    30.  
    31.     ' Returns the image codec with the given mime type
    32.     Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
    33.         ' Get image codecs for all image formats
    34.         Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
    35.  
    36.         ' Find the correct image codec
    37.         For i As Integer = 0 To codecs.Length - 1
    38.             If (codecs(i).MimeType = mimeType) Then
    39.                 Return codecs(i)
    40.             End If
    41.         Next i
    42.  
    43.         Return Nothing
    44.     End Function
    Last edited by MrPolite; Jun 5th, 2005 at 08:38 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Member
    Join Date
    Sep 2007
    Posts
    62

    Re: save JPEG images with any quality

    hm... i have a problem with these lines in vb.net 2008
    ublic Shared Sub SaveJpeg(ByVal path As String, ByVal img As Image, ByVal quality As Long)
    Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
    statement is not valid in a namespace

  3. #3
    Addicted Member Ashraf Alshahawy's Avatar
    Join Date
    May 2007
    Location
    Egypt
    Posts
    241

    Re: save JPEG images with any quality

    Hi MrPolite .... Good Topic

    Hi Jarck , the code need a little bet modification to work fine with you

    I tested it and works fine for me , make a new project and clear any lines before you paste the code :

    Imports System.Drawing.Imaging 'this statement must be at the topmost of any code inside a class.

    Public Class Form1

    ' Please do not remove
    ' Written by Kourosh Derakshan
    '

    'add this!



    ' 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

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'You can add this piece of code inside a Button_Click event or any event you like.

    Dim myImage As Image = Image.FromFile("E:\a7.jpg") 'Replace the "E:\a7.jpg" with path of your own image

    ' Save the image with a quality of 50&#37;
    SaveJpeg("C:\FFF.Jpg", myImage, 50) 'Replace the "C:\f7.jpg" with path where you want your image to be saved.

    End Sub
    End Class


    I hope that helped.
    Some times when you make a step forward ...You can't get back. Be sure where you make your steps.

  4. #4
    Member
    Join Date
    Sep 2007
    Posts
    62

    Re: save JPEG images with any quality

    ooh god....
    I know im noob...
    sorry for my stupid question

  5. #5
    New Member
    Join Date
    Jun 2015
    Posts
    1

    Smile Re: save JPEG images with any quality

    Quote Originally Posted by ashraf fawzy View Post
    Hi MrPolite .... Good Topic

    Hi Jarck , the code need a little bet modification to work fine with you

    I tested it and works fine for me , make a new project and clear any lines before you paste the code :
    I hope that helped.
    Absolutely helped me. I was debating converting images to grayscale and I was trying to avoid using DLLs that break between Windows versions (or have the potential to). I take an image, dither it down to 8-bit grayscale, and then use this code to save it at 10% (since it's just a reverse side of the check). The file size is 1/5 or less of what it would be otherwise. Thank you for saving me a lot of headache.

    Lauren

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width