Skip to content

Bitmap.Clone() doesn't always honor requested PixelFormat and thus results in Stackoverflow in Encode() recursion. #19

@crokusek

Description

@crokusek

Hi there!

Fyi, the following method generates a stackoverflow exception here due to recursion with certain PixcelFormats:

Inside SimpleEncoder.cs...

public void Encode(Bitmap b, float quality, out IntPtr result, out long length)
{
if (b.PixelFormat == PixelFormat.Format32bppArgb)
{
...convert...
}
else if (b.PixelFormat == PixelFormat.Format24bppRgb)
{
...convert...
}
else
{
// The problem occurs here with some input PixelFormat types.
// Notably PixelFormat = 8207 (undocumented non-enum value) occurs from streaming in a CMYK jpg
// https://stackoverflow.com/questions/5065371
// The bitmap.Clone() result below does not honor the requested the pixel format and
// returning the same PixelFormat type and so the recursive call ends up in this
// same block until a stackoverflow exception.

    // From this other related issue, there are other types then PixelFormat 8207 where 
    // this happens:  https://stackoverflow.com/questions/2016406
    //
    // To resolve, suggest using an GDI approach like the first answer here which 
    // uses Graphics to write into a new bitmap() for all pixelformats instead of Clone().
    // https://stackoverflow.com/a/2016509/538763        
    // I have verified the answer to work against 8207 (outside of the library code).

using Bitmap b2 = b.Clone(new Rectangle(0, 0, b.Width, b.Height), PixelFormat.Format32bppArgb);
Encode(b2, quality, out result, out length);    // RECURSE

}

And thanks for the library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions