I'm using the JPEG Encoder with a Bitmap.Save()-call.
I got the following code from the german MSDN:
ImageCodecInfo ici = null;
ImageCodecInfo iCodecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ic in iCodecs)
if (ic.MimeType == "image/jpeg")
ici = ic;
EncoderParameters ep = new EncoderParameters(1);
ep.Param = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (int)90);
Result.Save(CurrentTargetFile(), ici, ep);
After the execution of the last code line I get the following exception:
bei System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
Can anybody find any Problem? The filename parameter is 100% OK.
Thank you, that's it!
Is it just my impression or are these "special" topics really never dealt with in the VS online help?
I would at least expect a link "Description of the codecs"... not only an example (which doesn't work under some conditions) on the german MSDN website. To be honest I didn't read all trough ;-)
Well, I agree that generally the 'special' topics get little attention compared to the more common scenarios. To some respect that's understandable. We wouldn't want people hitting their heads constantly on database operations, ado.net, etc. so these topics are covered in more detail.
As for this particular case, there is some documentation here. But looking at it closely, there's a little issue. For one, it doesn't explain the paramater type. All it says, it has be between 0 and 100. That can easily fit in an int. Look at the VB.NET sample:
myEncoderParameter = New EncoderParameter(myEncoder, CType(25L, Int32))
Here, the parameter is specifically cast to an int. The C# sample, however says:
myEncoderParameter = new EncoderParameter(myEncoder, 25L);
The parameter is set as a long. Now, that's confusing, and from the looks of it, the VB.NET code is wrong. However, when we take a look at the compiled code, from the VB.NET sample:
Now, that's confusing right? The VB compiler simply ignores the cast to Int32. So CType or not, the VB.NET sample will also work.