[RESOLVED] convert BMP to JPG (VaryQualityLevel)
Hi there all Good People,
Please, please, please, please would anybody help me with this one? :cry:
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?
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
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.
Re: convert BMP to JPG (VaryQualityLevel)
vb Code:
Public Class JpegSaveClass
Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim j As Integer
Dim encoders() As ImageCodecInfo
encoders = ImageCodecInfo.GetImageEncoders()
j = 0
While j < encoders.Length
If encoders(j).MimeType = mimeType Then
Return encoders(j)
End If
j += 1
End While
Return Nothing
End Function 'GetEncoderInfo
Public Shared Function SaveImage(ByVal _bmpFilename$, ByVal _destinationFilename$) As Boolean
Try
If IsNothing(_bmpFilename$) Then Return False
Dim _btmp As New Bitmap(_bmpFilename$)
Dim jgpEncoder As ImageCodecInfo = GetEncoderInfo("image/jpeg")
Dim _encoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
Dim _encoderParameters As New EncoderParameters(1)
Dim myEncoderParameter As New EncoderParameter(_encoder, 50&)
_encoderParameters.Param(0) = myEncoderParameter
_btmp.Save(_destinationFilename$, jgpEncoder, _encoderParameters)
_btmp.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
how to use
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim _sourcefilename$ = "e:\18x12 001.bmp"
Dim _destinationfilename$ = "e:\18x12 002.jpg"
JpegSaveClass.SaveImage(_sourcefilename, _destinationfilename)
End Sub
End Class
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.
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.
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
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
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.
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... :blush:
it is resolved now.:thumb:
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.:check:
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:
Private Sub VaryQualityLevel()
' Get a bitmap.
Dim bmp1 As Image = Bitmap.FromFile("C:\original_file.bmp")
Dim img As New Bitmap(bmp1)
Dim jpgEncoder 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)
' here is a conversion from a NumericUpDown to long, so I could use any
' value I like for compression
Dim coverttolong As Long = Long.Parse(NumericUpDown1.Value)
' or use the below line of code to stick with one compresion level
' Dim myEncoderParameter As New EncoderParameter(myEncoder, 95&)
Dim myEncoderParameter As New EncoderParameter(myEncoder, coverttolong)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("C:\output_file.jpg", jpgEncoder, myEncoderParameters)
' next 4 lines might not be needed at all - not too sure about those
jpgEncoder = Nothing
myEncoder = Nothing
myEncoderParameter = Nothing
myEncoderParameters = Nothing
' this disposes the bitmap, so the original file can be used again
bmp1.Dispose()
' the below line is optional. You don't have to delete the original file as it can be overwritten
IO.File.Delete("C:\original_file.bmp")
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.