Unexpected exception "Raster area already locked" using GetEncoderParameterList. Any ideas?

I am trying to use this sample code from Microsoft to determine which encoder options are available for a JPEG encoder. (The real problem I'm trying to solve is to check if I can explicitly set Chroma subselect options)

http://msdn.microsoft.com/en-us/library/bb882589.aspx

private void GetSupportedParameters(PaintEventArgs e) { Bitmap bitmap1 = new Bitmap(1, 1); ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); EncoderParameters paramList = bitmap1.GetEncoderParameterList(jpgEncoder.Clsid); EncoderParameter[] encParams = paramList.Param; StringBuilder paramInfo = new StringBuilder(); for (int i = 0; i < encParams.Length; i++) { paramInfo.Append("Param " + i + " holds " + encParams[i].NumberOfValues + " items of type " + encParams[i].ValueType + "\r\n" + "Guid category: " + encParams[i].Encoder.Guid + "\r\n"); } e.Graphics.DrawString(paramInfo.ToString(), this.Font, Brushes.Red, 10.0F, 10.0F); } private ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return codec; } } return null; } 

The problem is that "GetEncoderParameterList" always throws an exception: the bitmap area is already locked.

I tried to put the code at the very beginning of my program, and not in the event handler in the language. Same. I tried to change the bit depth in the bitmap and create bitmaps in other ways, no difference.

Any idea why .NET thinks the newly created bitmap has a locked area?


Update! Additional information: if I use the TIFF encoder, it is not interrupted:

  Bitmap bitmap1 = new Bitmap(1, 1); ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.TIFF); // TIFF instead of JPEG EncoderParameters paramList = bitmap1.GetEncoderParameterList(jpgEncoder.Clsid); EncoderParameter[] encParams = paramList.Param; 

So, I think it could just be a GetEncoderparameterList error / limitation for jpeg ....

+4
source share
1 answer

I repeat this. The exception message is fictitious, which is common to GDI + exceptions. The source of the problem is the jpeg codec, GetEncoderParameterList () requests it for a list of supported parameters. The list that he returns contains one bad record, NumberOfValues ​​- 0. This causes Marshal.AllocHGlobal () to return NULL, which is incorrectly interpreted as an exception in memory, which is an incorrect interpretation because "the bitmap area is already locked" an exception.

Ugh, GDI + sure is a mess. This problem is almost certainly specific to GDI + version 1.10, the version that first ships with Vista. There may also be Win7, I saw several reports of problems with the JPEG codec in this operating system.

Well, there is nothing you can do about it until the next Windows service pack is available, if then. To answer your specific request, no, probably not. The list of encoder parameters that GDI + knows about is specified in the GdiPlusImaging.h SDK header file. The only thing close is the "EncoderChrominanceTable", available as Encoder.ChrominanceTable.Guid in the .NET framework. I don’t know how close it is to what you are looking for.

You should really consider the WPF class JpegBitmapEncoder, a significant improvement over GDI =.

+5
source

Source: https://habr.com/ru/post/1314364/


All Articles