C# - From native HBitmap to JPEG-encoded Stream, Transparency and upside down

713 views Asked by At

My final goal is to put a HBitmap, that I retrieved from the Windows Thumbnail Cache using this code:

// GUID of IShellItem.
Guid uuid = new Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe");
SHCreateItemFromParsingName(filename, IntPtr.Zero, uuid, out ppsi);
((IShellItemImageFactory)ppsi).GetImage(new SIZE(72, 72), 0, out hbitmap);

into a JPEG-encoded stream, that I will then send over the network.

My first attempt was to use Image.FromHBitmap(), but this one ignores the alpha channel. See this question: Use native HBitmap in C# while preserving alpha channel/transparency

The solution obviously is to create a new bitmap that uses the native bitmap's data. But please check the comments of Hans' answer to the other question: Some of the thumbs will be upside down, others won't. Is there any possibility to determine, if I have to rotate or not? (there is no answer to David's comment and I cannot comment there ...) I read many times, that it is usual that bitmap data is stored upside down, and the stride value would be negative then. I checked all fields of the NativeMethods.BITMAP object, and they all don't differ for normal and upside-down images. So there must be another solution.

There also is another problem: When saving the bitmap to a JPEG-stream with .Save(somestream, ImageFormat.Jpeg), the background shining through transparent parts of the image turns white, but I want it to be black.

Thank you in advance!

0

There are 0 answers