How to set image on transparent background in C # without using Bitmap.MakeTransparent ()?

I want the image to be transparent, but I do not want all the pixels of a certain color to be transparent.

To be more specific, the image is a thumbnail for the folder retrieved through IShellItemImageFactory.GetImage. This gives me a bitmap, as shown in a miniature representation of Windows Explorer, but the background is solid white.

I can use Bitmap.MakeTransparent on it, and this will work in most cases, but in any case, when the thumbnail image contains white itself (for example, a folder containing images containing white colors).

This is probably the first time in 10 years as a developer who, after I asked my question, I did not find an answer anywhere, and I really had to ask it myself. (I think that means I just tied the score! Yippee, now I'm a level 2 developer ...)

+4
source share
2 answers

Use the fill fill algorithm to fill the pixels of the same color from OUTSIDE as you need. This is something like a magic wand in Photoshop.

http://en.wikipedia.org/wiki/Flood_fill

What would I do is fill it with a kind of obscure color (Magenta always does this for me) and then replace that color with transparent (I don't know if filling with transparent pixels is possible with filling).

+1
source

So what do you get from IShellItemImageFactory.GetImage - is this a composite image that contains the original image on a white background? I suspect there is a white background if the image does not have the same format as the sketch size. For example, if you ask for a sketch with a resolution of 96x96 from 640x480 images, there will be some kind of white space above and below.

If this is the case, you have a problem. You cannot distinguish between white pixels contained in an image and white pixels that are added by GetImage .

There are a few things you could do. You can upload an image and resize it yourself. This is probably the easiest. You would like to save your own sketch.

Or you can check the image returned by GetImage to look for empty space around the sides. Basically, if every pixel in a row (or column) is white, make that row (or column) transparent. It is a bit more complicated than that ( NBA logo , for example). But this, in fact, is what you would like to do.

+1
source

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


All Articles