I've seen many questions asking how to draw transparent images, but my case is quite the opposite. I have a TPicture where I load any file type, including PNG. I then read TPicture.Graphic and call Draw directly in a TBitmap's canvas. However, when the image is drawn, it carries over the transparency of the original PNG image.
The current code is very simple, just...
MyPicture.LoadFromFile(SomeFilename);
MyBitmap.Canvas.StretchDraw(SomeRect, MyPicture.Graphic);
Now the issue is that the canvas which I'm drawing to already has an image, and this PNG is being drawn over a portion of it. When the PNG has a transparent background, normally it appears white. However, since it's directly drawing a transparent graphic to the canvas, it keeps those areas transparent.
How can I draw a PNG Graphic directly to a canvas without its original transparency while using only the canvas drawing methods? I don't want to create too many graphic objects and draw too many times, hence the reason I only have 2 lines of code above. I'm hoping there's a way I can do something like BitBlt with some special mechanism for this purpose.
The only method pre-built in Delphi XE2 has a defect and doesn't work properly. Instead, draw whitespace, or whatever background you desire, to a blank canvas. Then draw the transparent image on top.
In case you aren't drawing onto a blank canvas, you can call FillRect method of the bitmap canvas for the region you're planning to draw the png.
Related
I have been designing an app with oCanvas.js. It's a really nice canvas library that makes it much easier to create an app that can create and manipulate images, but I ran into a snag when I was trying to implement image filters:
I need transparent backgrounds so that I can have multiple layers, each of which is represented by its own display object, rendered separately (meaning one at a time) on a hidden "staging" canvas. Immediately after being rendered, a layer is then drawn on top of the previous layers on the visible canvas, so that different image filters can be applied to each layer independently during render.
The issue I am having is that, when attempting to extract the image from an oCanvas object's canvasElement, the resulting images never have a transparent background. For example: Imagine I have a 50x50 canvas that has been oCanvas.create() processed, but has display: none; (this is used as the rendering canvas) and another canvas (same dimensions) without an oCanvas instance. I am trying to do something like this (Pseudocode):
visibleCanvas.getContext("2d").drawImage(MyOcanvasCore.canvasElement, 0, 0);
I have also tried using URL = MyOcanvasCore.canvasElement.toDataURL() and then having my visibleCanvas do a drawImage with src=url.
The images always transfer, but they have a white background, even though I specify background: "transparent" during canvas.create(). As such, they completely overwrite all previous layers.
Do you have any tips for me? Am I doing it wrong? I tried transferring stuff from one canvas to another using classic drawRect, drawImage, etc methods, and transparency was retained. That's why I believe it is either the library or my code.
I guess you are using the image format other than png.
You should have your image format in PNG which keeps all the details of every pixel >including transparency of pixels
and not a compressed format.
So just try keep your image in png format after edting them in some image editor and save > result as .png .
i wonder, how i could figure out if an image has a transparency effect applied. Is there any way in JavaScript or HTML5? I have a Base64-coded image. Is there a way to read out the transparency-information (alpha-channel). For example, if i load a PNG-Image, then convert it to base64, then drop it to html5-canvas, now how can i know if this has transparency-effect activated?
thanx alot
okyo
When you say 'drop it to html5-canvas', I assume you mean using an image element with the 'data:' URI scheme. Also, let's take it as given that you don't want to write javascript code to parse the image files.
You could do something like this pseudo-code:
create 2 off-screen canvases
color one opaque white and the other opaque black
draw the image on both of them
call getImageData on each canvas, using the image bounds
compare the image data
If the image has any transparent or partially-transparent pixels, then presumably the two canvases will end up at least a little different. One exception would be if the image has the transparency feature enabled but is entirely opaque anyway. Another would be if the non-opaque pixels are only very slightly transparent - not enough to alter a white or black background. But this technique would catch images where transparency is noticeable.
How to get a TPicture from a TImageList?
I need to do Image1.Picture:=...TPicture from an image list, to load an image into a TImage.
An image list stores all my PNG images that are transparent.
I tried to use a bitmap (GetBitmap), but what I need is transparency. Unfortunatelly, I have a white background using a bitmap.
Thanks!
The regular TImageList uses bitmaps. Although they can be partially transparent, it is actually just a fake. In Delphi you can draw bitmaps to be transparent by assigning them a single transparent color. That exact color will be drawn a 100% transparent, while the other colors are not. Usually the color is taken from the bottom left corner of the image.
TPicture itself doesn't do anything. It is merely a container for TGraphic descendants. You'll have to find a type of image that can be transparent.
A convenient format is PNG. PNG event supports an alpha channel, which means that every pixel can be assigned a different transparency value.
Fortunately there are TPngImageLists that combine the ease of TImageList with the power of PNG. You can read this article. It is in Dutch, but maybe Google Translate can help you. Or maybe you can find an english resource on this subject. I've used this imagelist and it's great, because you can have actual icons with an alpha channel and still use them with regular toolbars and speedbuttons.
Normally I'm using CALayer shadowRadius, but now I also need to use UIImage and apply shaped shadows to it based on the content in the image.
For example when I have a layer with text in it and I set a shadow, it works automatically on the text and not just on the rectangle of the layer.
In Photoshop this is known as "layer style" and it automatically works based on the shape of the image content.
I am afraid that I need to implement some Harvard-Stanford-MIT-NASA kind of hardcore logic to apply a shadow on a "shaped image", i.e. an image of an round icon where the areas around the icon are fully transparent.
I'm able to manipulate images on a per-pixel level as I'm doing this already to draw charts, so if there was an open-sourced implementation of some fantastic algorithms this would be fantastic. And if not: How does this basically work? My guess is I would "just" try to blur a grayscaled version of my image somehow and then overlay it with the non-blurred version.
My guess is I would "just" try to blur a grayscaled version of my image somehow and then overlay it with the non-blurred version.
That's pretty much it, actually. Except instead of blurring a greyscaled version of the image, blur a solid-colored version of the image (i.e. keep the alpha channel, but make all pixels black). Although CALayer's shadowing should do this already for you.
If your images are already composited onto a background (i.e. without real transparency), you have a harder problem as you first need to "remove" the background before you can have the shape of the object in order to generate the shadow.
I have a PNG image that uses transparency (it is actually a circle with gradient effect from black in the middle, to transparent on the margins). I am putting this on a form using TImage. I set TForm1.Color and the TForm1.TransparentColorValue to the same value and TForm1.TransparentColor:=true.
Now, when I run the program the gradient part of the image is displayed with the color of the form. What I am looking for is to enable the transparency of the PNG image using the transparent form effect.
What am I doing wrong?
I am using Delphi 2010 Trial.
I suspect you're trying to create something like a transparent splashscreen, if that's the case, you can read these great articles, they describes a nice way to use a transparent png in a delphi form.
Alpha Blended Splash Screen in Delphi - Part 1
Alpha Blended Splash Screen in Delphi - Part 2
Bye.
Your settings are wrong. I am doing this (With a bitmap).
The TImage.Transparent should be false.
The Form.TransparentColourValue should be the colour of the part of The TImage that you want to be transparent.
The Form.TransparentColor should be True.
[Edit]
It does not matter what colour the form is if the Image covers it completely