Results 1 to 10 of 10

Thread: [RESOLVED] convert BMP to JPG (VaryQualityLevel)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Location
    Glogow, Poland
    Posts
    104

    Resolved [RESOLVED] convert BMP to JPG (VaryQualityLevel)

    Hi there all Good People,

    Please, please, please, please would anybody help me with this one?

    I'm using an example code from msdn in my project to convert a BMP file to JPG file.

    the code comes from here: http://msdn.microsoft.com/en-us/libr...90%29.aspx#Y74

    this code allows you to pick quality/compresion level of the output JPG file.

    this is how it looks like:
    Code:
    Private Sub VaryQualityLevel()
        ' Get a bitmap.
        Dim bmp1 As New Bitmap("c:\TestPhoto.jpg")
        Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
    
        ' Create an Encoder object based on the GUID
        ' for the Quality parameter category.
        Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
    
        ' Create an EncoderParameters object.
        ' An EncoderParameters object has an array of EncoderParameter
        ' objects. In this case, there is only one
        ' EncoderParameter object in the array.
        Dim myEncoderParameters As New EncoderParameters(1)
    
        Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
        myEncoderParameters.Param(0) = myEncoderParameter
        bmp1.Save("c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters)
    
        myEncoderParameter = New EncoderParameter(myEncoder, 100&)
        myEncoderParameters.Param(0) = myEncoderParameter
        bmp1.Save("c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters)
    
        ' Save the bitmap as a JPG file with zero quality level compression.
        myEncoderParameter = New EncoderParameter(myEncoder, 0&)
        myEncoderParameters.Param(0) = myEncoderParameter
        bmp1.Save("c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters)
    
    End Sub 'VaryQualityLevel
    
    
    
    ...
    
    
    Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
    
        Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()
    
        Dim codec As ImageCodecInfo
        For Each codec In codecs
            If codec.FormatID = format.Guid Then
                Return codec
            End If
        Next codec
        Return Nothing
    
    End Function
    Now... for some reason it locks the original file ("c:\TestPhoto.jpg" in the example above), so you cannot use it for some time - sometimes many minutes. If you try to delete the file you get an error message saying it is being used by another process. You cannot overwrite it/rename it/move it... If I close my program and reopen it immediatelly the file becames available again, so closing the program somehow kills the process, but I have no idea what kind of process it might be.

    What is missing in the above code? How do I identify and kill this process? How do I make the original file available again?

    Any ideas?

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: convert BMP to JPG (VaryQualityLevel)

    I'm surprised too because I thought creating a new bitmap from a file wouldn't keep the file tied up. But it does. For what it's worth, the following code worked and allowed an immediate delete so it should work in your situation too:
    Code:
    Dim img As Image = Bitmap.FromFile("D:\Pictures\Test.png")
    Dim bmp As New Bitmap(img)
    img.Dispose()
    IO.File.Delete("D:\Pictures\Test.png") 'worked without error
    
    BB

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Location
    Glogow, Poland
    Posts
    104

    Re: convert BMP to JPG (VaryQualityLevel)

    Thanks for the reply BB, but unfortunatelly this did not worked for me.

    Let me explain in more detail what I do.

    I'm taking a screenshot of a webbrowser area in my form. This is being saved to BMP file. Then I convert the BMP file to JPG with a chosen compression level. For some reason the BMP is being locked by the code presented by me.

    I'm sure that this is the code that is responsible for locking the file, because without conversion to JPG the BMP file can be overwritten/deleted/moved without issues. As soon as I apply the conversion to JPG it gets locked for several minutes.

    Any other ideas ? If you can think about any other solutions, please reply.

  4. #4
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: convert BMP to JPG (VaryQualityLevel)

    vb Code:
    1. Public Class JpegSaveClass
    2.     Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
    3.         Dim j As Integer
    4.         Dim encoders() As ImageCodecInfo
    5.         encoders = ImageCodecInfo.GetImageEncoders()
    6.         j = 0
    7.         While j < encoders.Length
    8.             If encoders(j).MimeType = mimeType Then
    9.                 Return encoders(j)
    10.             End If
    11.             j += 1
    12.         End While
    13.         Return Nothing
    14.     End Function 'GetEncoderInfo
    15.  
    16.  
    17.     Public Shared Function SaveImage(ByVal _bmpFilename$, ByVal _destinationFilename$) As Boolean
    18.         Try
    19.             If IsNothing(_bmpFilename$) Then Return False
    20.  
    21.             Dim _btmp As New Bitmap(_bmpFilename$)
    22.             Dim jgpEncoder As ImageCodecInfo = GetEncoderInfo("image/jpeg")
    23.             Dim _encoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
    24.             Dim _encoderParameters As New EncoderParameters(1)
    25.             Dim myEncoderParameter As New EncoderParameter(_encoder, 50&)
    26.  
    27.             _encoderParameters.Param(0) = myEncoderParameter
    28.             _btmp.Save(_destinationFilename$, jgpEncoder, _encoderParameters)
    29.             _btmp.Dispose()
    30.  
    31.             Return True
    32.         Catch ex As Exception
    33.             Return False
    34.         End Try
    35.     End Function
    36.  
    37. End Class

    how to use

    vb Code:
    1. Public Class Form1
    2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.         Dim _sourcefilename$ = "e:\18x12 001.bmp"
    4.         Dim _destinationfilename$ = "e:\18x12 002.jpg"
    5.  
    6.         JpegSaveClass.SaveImage(_sourcefilename, _destinationfilename)
    7.     End Sub
    8. End Class

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Location
    Glogow, Poland
    Posts
    104

    Re: convert BMP to JPG (VaryQualityLevel)

    thanks for the reply medsont.

    I'll try your code tonight (that is in about 10 hours time) and I'll post the result here afterwards.

  6. #6
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: convert BMP to JPG (VaryQualityLevel)

    Hi marl, do you need to make a bitmap file? You could take the screenshot in your project and save it straight to a jpeg.

  7. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: convert BMP to JPG (VaryQualityLevel)

    @marl: I don't know why you couldn't get my example to work but maybe you misunderstood it. The main thing is to dispose of the bitmap (or image) after use, because it is directly linked with the file.

    An alternative way of doing it would be to put
    Code:
    bmp1.Dispose
    at the end of the VaryQualityLevel sub from post #1. Medsont's code ought to work too, since it also disposes of the bitmap after saving.

    BB

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Location
    Glogow, Poland
    Posts
    104

    Re: convert BMP to JPG (VaryQualityLevel)

    @ jay20aiii - I attempted to save file straight into JPG, but there were issues with quality, therefore I decided to save it first to BMP (full quality) and then convert it to JPG, which created better looking JPG images.

    @boops boops - I did tried to dispose the bitmap. Not only the bitmap - I got desparate and have been tring to clear/dispose everything . For some reason it did not worked. I get an exception while trying to dispose the bitmap 'Object reference not set to an instance of an object.'

    @ all - I acutally compiled my code with this converter months ago. I spotted the issue, but assumed it will be easy to dispose the bitmap or force a deletion of the temporary BMP file, but once I started using it recently I was surprised it cannot be done.

    I did not had a chance to try o closely look at code pasted by medsont - busy day at work - I believe I will be able to check it tonight.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Location
    Glogow, Poland
    Posts
    104

    Re: convert BMP to JPG (VaryQualityLevel)

    screw work - just had another go and it worked.

    boops boops - you were right - your code works, but you have to remember to remove all the other rubbish you put in there before in desperate attempt to resolve the problem without asking for help on the forum. Silly me...

    it is resolved now.

    Thanks a lot to everybody how replied. I will post the full code with updates in the evening and then I will mark the thread resolved.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Location
    Glogow, Poland
    Posts
    104

    Re: convert BMP to JPG (VaryQualityLevel)

    Hi.

    Lets close this thread. this is my code with some comments. Please note it has been amended to suit my needs. Please note that I'm usuing a value from NumericUpDown to pick the quality level of the output file.

    vb Code:
    1. Private Sub VaryQualityLevel()
    2.         ' Get a bitmap.
    3.         Dim bmp1 As Image = Bitmap.FromFile("C:\original_file.bmp")
    4.         Dim img As New Bitmap(bmp1)
    5.         Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
    6.  
    7.         ' Create an Encoder object based on the GUID
    8.         ' for the Quality parameter category.
    9.         Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
    10.  
    11.         ' Create an EncoderParameters object.
    12.         ' An EncoderParameters object has an array of EncoderParameter
    13.         ' objects. In this case, there is only one
    14.         ' EncoderParameter object in the array.
    15.         Dim myEncoderParameters As New EncoderParameters(1)
    16.        
    17.        
    18.         ' here is a conversion from a NumericUpDown to long, so I could use any
    19.         ' value I like for compression
    20.         Dim coverttolong As Long = Long.Parse(NumericUpDown1.Value)
    21.        
    22.         ' or use the below line of code to stick with one compresion level
    23.         ' Dim myEncoderParameter As New EncoderParameter(myEncoder, 95&)
    24.         Dim myEncoderParameter As New EncoderParameter(myEncoder, coverttolong)
    25.         myEncoderParameters.Param(0) = myEncoderParameter
    26.         bmp1.Save("C:\output_file.jpg", jpgEncoder, myEncoderParameters)
    27.  
    28.        
    29.         ' next 4 lines might not be needed at all - not too sure about those
    30.         jpgEncoder = Nothing
    31.         myEncoder = Nothing
    32.         myEncoderParameter = Nothing
    33.         myEncoderParameters = Nothing
    34.        
    35.        
    36.         ' this disposes the bitmap, so the original file can be used again
    37.         bmp1.Dispose()
    38.  
    39.        
    40.         ' the below line is optional. You don't have to delete the original file as it can be overwritten
    41.         IO.File.Delete("C:\original_file.bmp")
    42.        
    43.     End Sub 'VaryQualityLevel

    so... this is it. Once again thanks a lot for all replies.

    some tags for search engines: bmp to jpg conversion, convert bmp to jpg, jpg quality, jpg compression, bitmap, jpeg


    The thread is resolved.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width