Windows button ignores alpha channel for fully transparent image - winapi

I have a radio button that should display an image (style BS_AUTORADIOBUTTON|BS_PUSHLIKE|BS_BITMAP).
I create a bitmap via CreateDIBSection (using a BITMAPINFO with BI_RGB) and obtain a pointer to the raw pixels via the ppvBits, so that I can manipulate them freely.
I use BM_SETIMAGE to set the button's image to the bitmap I created.
So far, I can set the RGB and alpha by manipulating the pixels by hand. I tested that even semi-transparent (non-premultiplied) alpha values look good.
As far as I can tell, everything works, except if all pixels in the image are transparent. In that case, the button apparently ignores the alpha value, simply displaying a rectangle with each pixel having the respective color with full opacity.
I found a hint that Windows - at least in some cases - actually seems to interpret images whose pixels' alpha values are all 0 as completely opaque images:
When the window manager sees a 32bpp bitmap, it looks at the alpha
channel. If it's all zeroes, then it assumes that the image is in 0RGB
format; otherwise it assumes it is in ARGB format
Is this behavior documented somewhere?

Is this behaviour documented somewhere?
Yes! In Raymond's post! :) That's often the way of it, no?
If you look at the foot of the page here you will find a comments box. If you raise your concerns there then MS will most likely fix their documentation. See here for an example of the process they usually follow if they consider the problem serious enough to fix.

Related

Direct2D: smooth image movement?

Im using direct2d to draw a bitmap to the window, and thats working fine, but when I move the image (continually, with joystick, key input, or just by itself), it doesn't move smoothly. It look's like the image pixels are not spread to surrounded pixels, It looks like the most dominant colors remain, and the others are discarded. I create the bitmap using WIC (windows imaging component).
I didn't want to post code cause it's just the most basic stuff, window creation, direct2d initialization, bitmap creation and drawing, all taken from msdn, most of it is just copy-paste. If you think you need any code to help me, ask, then I'll post it.
What I want:
Smooth image movement on picture.
What I've tried:
-void SetAntialiasMode(D2D1_ANTIALIAS_MODE antialiasMode); (tried all options)
-setting D2D1_BITMAP_INTERPOLATION_MODE in draw bitmap method to different modes.
Thanks in advance!

Delphi PNGImage - Image transparency is not pure

I am using PNG images as main image resource in my application. Since im using Delphi 7, i downloaded PNGImage lib and included it in project. I load images like this:
Form.image.Picture.LoadFromFile(PAnsiChar('\background.png'));
Image has transparent and semi transparent pixels on its border. The problem i get is that transparent pixels are filled with random zoomed part of my desktop with currently opened windows, while i expected to see what is actually located beneath form.
Additionally, form has this properties:
BorderStyle: bsNone;
TransparentColor: true;
Visible: false;
Here is a picture of current state (above black line) and desired:
Can this be fixed somehow or it is how delphi deals with transparency?
To have the form "shade" what's beneath it, use the forms AlphaBlend and AlphaBlendValue properties. The .png image doesn't have to be partially (alpha blended) transparent, but it can be.
If you want the form to be semi-transparent you use Alphablending, that's a limitation of Windows. In addition you can have a certain color fully transparent. In the following sample the forms color is clGray, which is also defined as the Transparent color property in addition to the Transparent property set to True. The image, aligned alClient, is 50% transparent, placed on a TImage which is set as transparent, but even so, it doesn't show up as semi-transparent unless you have AlphaBlending on. Again, this is a limitation of Windows. The best you can do is try with a fairly high value for AlphaBlendingValue (240..250) and a rather light image to find the right compromise.

Displaying Icons stored as resources with alpha using GDIPlus (WIn32 C++)

I have an icon with partial alpha (alpha values between 0 and 255) that I want to display using GDIPlus. When using the Bitmap constructor of GDI+ that takes the direct filename, the file displays properly. However, when loading from resource, it has a problem recognizing alpha. I looked on MSDN, and there are problems with alpha: http://msdn.microsoft.com/en-us/library/windows/desktop/ms536318.aspx. By retrieving the ICONINFO structure from the Icon, I can get rid of the fully transparent pixels, however, the partially transparent pixels still appear either as fully opaque or fully transparent.
I wanted to know how to create a Win32 Bitmap from an Icon in resource with the partial alpha values.
You can use LoadResource to get a pointer to the icon and and its image data. You can pass the pointer to the image data to the appropriate Bitmap constructor. This is a bit of a chore because icons have a peculiar resource format.
If possible, it would be simpler to store your image as a transparent (i.e. 32bpp argb) bitmap. In this case you can use LoadImage with LR_CREATEDIBSECTION.
Update
Apparently LoadIcon does load the alpha correctly. It would appear that the problem is GdiPlus not respecting the alpha when you construct a GdiPlus::Bitmap from an HICON. What you could do is:
Use LoadIcon to load the icon.
Use GetIconInfo to get the ICONINFO. hbmColor is the handle of the transparent bitmap.
Use GetDIBits to get the bitmap bits from hbmColor.
Pass the data to the Bitmap constructor that takes bits and understands alpha.
The alpha channel is disturbed after you call LoadIcon. The Win32 APIs that load icons, e.g. LoadIcon, LoadImage, etc. are well proven. They reliably load icons with partial alpha.
You need to investigate the code that executes after the icon has been loaded. I can't give you a solution or an explanation, but I am confident that LoadIcon is not the culprit.
I wanted to know how to create a Win32 Bitmap from an
Icon in resource with the partial alpha values.
Call GetIcon or GetImage to obtain an HICON. Then call GetIconInfo. The bitmap you need is in the hbmColor field of the ICONINFO struct.

Delphi: 32-bit image does not display correctly on Glass Form

I have a PNG (32-bit) image in a TImage. A form has a Glass Frame. Picture's background is black, not transparent. How to fix it?
Delphi 2010.
Thanks.
As workaround you can change the bits of your png image to 32.
Check this sample
the left image is a PNG of 8 bits and the other is of 32 bits.
You should forget about the TImage as it won't handle the blit right. You need to use GDI+ to manually draw the image on your form. That's only because of the "glass". IF you leave it up the the TImage (or actually the TGraphic displayed in it!), the "background" (glass) will be copied into memory and onto that, a transparant graphic (png?) will be composed, and blitted back tot he form. Unfortunately, the "background" (the glass) will turn out to be black when you blt it back.
So, use GDI+ (google it) and blt using the bitmap.handle. Make sure the bitmap is transparent (i.e. 32bits and the appropriate properties set).
Same with fonts on glass, btw. You have to draw stuff yourself (maybe from a custom component?). You can find a few components that do this already, though... Like http://development.mwcs.de/glowlabel.html.

How to watermark a dynamically generated PDF so watermark seen through non-transparent cells

I have a dynamically generated PDF that can be in one of two modes. If in a draft mode, the only real difference is a watermark that states DRAFT on each page.
I am using a page event, so on the startpage I print the image, so that it is under everything. When I did this on the endpage event then it would be on top of everything, which was bad.
I would like to have alternating colors on the tables so that it is easier to see, but if I set the gray background to be 0.9 then it is opaque and the watermark is covered. If I set the background color to be null and the gray color is 1.0 then it is transparent.
So, my question is, on the non-white alternating rows can I get the watermark to be seen.
Ideally it would be nice to be able to place the image on top of the layer with the borders and background colors, so it would be under the text, but, it appears that isn't an option, as, when I use the stamp with the under content layer it would be under all the cells, since they all had colors set, so nothing was transparent.
UPDATE:
I haven't tried this yet, but it appears that if I save the PDF then using the PdfStamper I can save the graphics state, set the opacity for the image, then restore the graphics state, and it may work.
http://www.vbforums.com/showthread.php?t=518686
But, I don't see any way to do what I would like without saving it first, as I am actually generating and streaming directly to the browser, and would prefer not to save, if possible.
By using the link in my final update I was able to get the watermark to work.
So, I save the file, then I use a MemoryStream to read in the file and I save the graphics state, set the opacity, set the watermark and then restore the graphics state.
I then stream this back to the browser and delete the file.
I haven't found any way to do it that works better than this. What is nice is that an image is not needed for the watermark, so I set the font size to 96pts, and that seemed to work fine, in landscape and portrait mode.

Resources