Delphi XE7 - Make PNG image transparent inside Form - image

I set the Form Color to : clFichsia and TransparentColor: True and TransparentColorValue to : clFuchsia.
I need to remove the clFuchsia color from the Form.
Normally the image is transparent but when i load it in the application
the color of the Form doesn't disappear.
How my application looks like:

There are two different ways to handle transparency:
1) the old simplified (Windows/Delphi) way, where a specific RGB color is made transparent. This method does not allow any partial transparency (opacity is either 0% or 100%).
2) the "new" way, where you use an image with an alpha-channel, e.g. a PNG image. Each pixel has an opacity value in addition to the RGB value, which allows partial transparency (e.g. for anti-aliasing).
You are mixing those two methods. You load a PNG image with an alpha channel (method 2), but then you also set a transparent color (method 1). And the reason it looks that bad, is because the "fuchsia" RGB value in your image is not exactly the same everywhere.

Related

Windows button ignores alpha channel for fully transparent image

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.

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.

Convert StdPicture Transparency Colour to White

I am using a CodeJock ImageManager component to hold a variety of images. I want to put one of these images into a FlexGrid Cell.
The Images I have are Png format and have transparent backgrounds so when I load the image into the grid like so:
Grid.Cell(flexcpPicture, 123, 4) = _
ImageManagerControl.Icons.GetImage(ImageNum, 16).CreatePicture(xtpImageNormal)
the background which in the original image was transparent is now black:
(the same happens if I load the image into a PictureBox using the above method)
According to the documentation CreatePicture returns an StdPicture object, is there some way I can convert this image (using BitBlt perhaps) so that the black is white? I'm not sure if this even possible?
I only need to do this with about three images so if I have to load them into an ImageList or something else first that would be ok.
I'm not sure if this helps at all, but I've been playing around with the PictureBox. I put two picture boxes on a form, put a bmp file (this only works with bmp files, so it might not be helpful to you), and did this:
Picture2.PaintPicture Picture1.Picture, 0, 0, opcode:=vbNotSrcCopy
Which successfully inverted the bitmap. Here are the RasterOp constants: http://msdn.microsoft.com/en-us/library/aa243035(v=vs.60).aspx

java & SWT - how to use masking on images

i'm trying to use masking on an image in order to set a part of the image to be transparent.
from what i've read , it's supported by the SWT library , but i can't find a way to do it . i can't find even an example for that .
i've therefore created my own way to do it , by implementing the masking part :
first i find an RGB value that isn't taken anywhere on the image , and then i set it to be the transparency color. then i take the masking image , and use it to put the correct pixel (either transparent or taken from the source image) in order to create a new image.
it works , but i wish i had a simpler method .
can anyone please help?
This constructor on the Image class looks to do the job:
public Image(Device device,
ImageData source,
ImageData mask)
from the javadoc...
Constructs an instance of this class, whose type is SWT.ICON, from the two given ImageData objects. The two images must be the same size. Pixel transparency in either image will be ignored.
The mask image should contain white wherever the icon is to be visible, and black wherever the icon is to be transparent. In addition, the source image should contain black wherever the icon is to be transparent.
Parameters:
device - the device on which to create the icon
source - the color data for the icon
mask - the mask data for the icon

How to read the original alpha channel from PNG in J2ME?

I'm writing a simple J2ME game that uses PNG images with 8-bit alpha channel. Problem: not all hardware supports full alpha transparency rendering. However, since my game is pretty static in nature (at the beginning, "sprites" are layed out onto background image, based on current screen size, and that's about it), I thought it would be possible to prerender those transparent images directly onto background during game initialization and use that later in game. I can't prerender them in Photoshop as their positions are not known in advance.
But, it seems there is no way to read the original alpha channel on devices that do not support semi-transparency as it gets resampled during PNG loading. Is there some library that can help with that? Or is it a good idea to store alpha channels separately (e.g. as separate 8-bit PNG images) and manually apply them?
Thanks!
PNG Images also have transparency support if you want to create transparent image then you have read RGB data along with alpha channels and process alpha
Image transPNG=Image.createImage("/trans.png"); //load the tranparent image
int rgbData[];
transPNG.getRGB(rgbData, 0,transPNG.getWidth(), 0, 0,transPNG.getWidth(), transPNG.getHeight());
Image tranparentImage=Image.createRGBImage(rgbData, width, height, true); //process alpha
transPNG=null;
Above code shows how to create the transparent image and use.
I cant promise this will help, but you can try this way of reading the alpha channel using standard methods from Java util.
BufferedImage image = ImageIO.read(new File(name));
int[] alpha = new int[1]; //containg alpha-value for one pixel.
image.getAlphaRaster().getPixel(x, y, alpha);
System.out.println(alpha[0]); //gives the alpha value for x,y

Resources