1

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!

Community
  • 1
  • 1
Philipp
  • 53
  • 5
  • For the second part of your question, the solution is do the matting yourself by making a second black-filled bitmap the same size as the original, and blitting the original onto it (since JPEGs don't do transparency). – Mike Caron Feb 27 '11 at 14:10
  • 1
    Thank you. This is exactly how I ended up doing it. As there several hundred of those thumbnails to be processed (as quickly as possible), this slows everything down a little bit, but it seems to be the only solution. For the other problem, the solution is first requesting thumbnails only. If one is returned, turn it upside-down and use it, if an error is returned, there is no thumbnail, so send another request for an icon, perform no turning and use it right away. Additionally, I found, that only the icons lack the black background, so all the image-processing is only necessary for those. – Philipp Mar 29 '11 at 08:28

0 Answers0