Note that there are some explanatory texts on larger screens.

plurals
  1. POUnexpected "Bitmap Region is already Locked" exception with GetEncoderParameterList. Any ideas?
    primarykey
    data
    text
    <p>I'm trying to use this sample code from Microsoft to determine what encoder options are available for the JPEG encoder. (The real problem I'm trying to solve is to see if I can set the Chroma subsampling parameters explicitly)</p> <p><a href="http://msdn.microsoft.com/en-us/library/bb882589.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/bb882589.aspx</a></p> <pre><code>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 &lt; 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; } </code></pre> <p>The problem is, "GetEncoderParameterList" always throws an exception: Bitmap Region Is already Locked.</p> <p>I tried putting the code at the very beginning of my program, and not in an on-paint event handler. Same thing. I tried changing the bit depth on the bitmap, and creating bitmaps in other ways, no difference.</p> <p>Any idea why .NET would think a freshly created bitmap has a locked region?</p> <hr> <p>Update! Some more info: If I use a TIFF encoder, it doesn't fail:</p> <pre><code> 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; </code></pre> <p>So I think this may just be a bug/limitation of GetEncoderparameterList for jpeg....</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload