How do I print a partially transparent image to a PDF Canvas using Delphi? - delphi

My program needs to output a (fairly complex) form to the printer, including several images. I’m currently doing this using Delphi (2006)’s Printer.Canvas, after selecting a PDF printer (PDF995). This works like a treat.
However, I’m now running into a problem: there’s one partially transparent image that needs to be placed on top of other elements (borders, background and such), with portions of that text still visible through parts of the image.
Doing this on a regular screen Canvas works fine with regular TBitmaps, by using the TransparentColor property. However, when I try to do this on a printer, it doesn’t always work; and when I try this on a PDF printer, it never works: the background turns black, or (the best result so far), turns white, but still overwrites anything underneath it.
I’ve tried achieving the same result by inserting a PNG image with alpha transparency (a.k.a. translucency) in a Word document, and then sending that to said PDF printer. The translucency disappears, but pixel transparency is maintained. So that, at least, should somehow be possible.
Anybody know how?

You mention you are using the TransparentColor property, so is it correct to say you don't really need alpha transparency and can get away with using binary transparency (transparency for each pixel is either off or on)?
If so, it might be possible to generate a Region from your bitmap.
You then use this region as a clipping region and draw the bitmap.
The PDF printer might be able to handle a clipping region correctly.
Here's an example of generating a region from a bitmap, it's used to make non-rectangular forms but the idea is the same:
http://www.delphi-central.com/BitmapShapedForm.aspx
Here is another example of setting a clipping region for your TCanvas:
http://www.efg2.com/Lab/OtherProjects/PrinterDemo2.htm

I don't think you can achieve partial transparency using a pdf printer. What you can do is get the VisPDF package. With it you can add a mask to the images contained in the pdf that you actually produce right in you're application. If this is not an option, you could combine all your background stuff in to one image. Drawing the alphachanneled thing on this image.

I also had this problem before, If I recall correctly, what I ended up doing was to create an off screen bitmap that I manipulated, then after I was done copied again and used the new bitmap copy on the canvas I was sending to the printer.

You may want to consider using a third-party PDF component in your application to render the canvas directly to PDF. I use this technique for specialized reporting (RTF, Radioemtric JPEGs, Tables, Text etc) in a commercial product I developed for Infrared Thermography. I am very happy with the performance and quality. The component I am using llPDFLib was just updated and I believe now supports full image transparency.
Best of luck. Printing images with an Alpha channel can be challenging at times in my experience.

Related

Painting issues with TScaledLayout & custom styles

I'm experiencing painting issues when combining TScaledLayout and custom styles created from the bitmap style designer in fmx.
To demonstrate, I loaded the default custom style created by chosing "New style for VCL / FMX" -> "save as .style" in the bitmap style designer. I dropped several standard controls on some colored rectangles: The red & green ones on a TScaledLayout, the blue one directly on the form. As I stretch the form, colored lines appear on the controls on the ScaledLayout; the background is partially visible:
If I size the form to exactly match the design-time dimensions, the lines disappear. That seems like a pretty significant issue, I certainly can't use those two together like that. Does anybody have an idea for a possible fix or workaround?
Looks like this is a known issue with scaling and bitmaps. See the Google+ discussion here - https://plus.google.com/+PaulThornton/posts/ACAHkJD3a84. I'll quote Marco Cantu's thoughts:
I've found an internally reported issue of a similar case, but haven't
found one that matches this scenario. Certainly worth adding to quality
portal. Having said this, I fear that bitmap-based operations and
scaling don't really fit together very well, and it might be difficult
to have an all encompassing solution.
Let me explain with an example. Take a button. This is painted by FMX
with 9 sections (borders, corners, central part) so that regardless of
the size the bitmap elements are stretched in one direction at most,
often just draw. Stretching a single bitmap for the button to the
target size would break anti-aliasing and create a blurred image when
using colors.
This is example what happens with a ScaledLayout, given it takes the
complete final image and transforms it. ScaledLayout was originally
introduced with vector styles, and worked very well in that scenario.
With todays's bitmap styles things get a bit more complex.
Regardless of this explanation of there the issue lies, I'd recommend
reporting it on QC, and I'll make sure it doesn't get closed as design
(it could naturally happen, this is how the system works) but that we
do some investigation to address the issue -- turning this into a
feature request.

Does EMF (Enhanced Metafile) support opacity?

I would be able to create an EMF image (programmatically or from a software) having the background color 50% transparent and the other elements completely opaques.
I'm already be able to create a background completely transparent, but not with a certain alpha/opacity.
Creating this semi-transparent background would allow to overlay the image to another background without completely hide it.
Does anyone know if it's possible with the Enhanced Metafile format?
The answer is no: EMF (or WMF) image format supports full transparency but not opacity between 0.0 and 1.0 because it doesn't support colors with alpha channel.
Here are image formats that supports alpha channel
I've been struggling with this topic a few weeks now and my research led me to good results.
Here is information for those who might face this issue in the future:
Windows EMF which uses GDI library does not support transparent colors.
However there is a newer EMF format called EMF+, which uses GDI+ library and supports transparency.
Here is the documentation of the class.
There are quite a few examples for these metafiles in C++ arround the internet, but some experiments can lead you the right way.

Replace particular color of image in iOS

I want to replace the particular color of an image with other user selected color. While replacing color of image, I want to maintain the gradient effect of that original color. for example see the attached images.
I have tried to do so with CoreGraphics & I got success to replace color. But the replacing color do not maintain the gradient effect of the original color in the image.
Can someone help me on this? Is the CoreGraphics is right way to do this?
Thanks in advance.
After some struggling almost with the same problem (but with NSImage), made a category for replacing colors in NSImage which uses ColorCube CIFilter.
https://github.com/braginets/NSImage-replace-color
inspired by this code for UIImage (also uses CIColorCube):
https://github.com/vhbit/ColorCubeSample
I do a lot of color transfer/blend/replacement/swapping between images in my projects and have found the following publications very useful, both by Erik Reinhard:
Color Transfer Between Images
Real-Time Color Blending of Rendered and Captured Video
Unfortunately I can't post any source code (or images) right now because the results are being submitted to an upcoming conference, but I have implemented variations of the above algorithms with very pleasing results. I'm sure with some tweaks (and a bit of patience) you might be able to get what you're after!
EDIT:
Furthermore, the real challenge will lie in separating the different picture elements (e.g. isolating the wall). This is not unlike Photoshop's magic wand tool which obviously requires a lot of processing power and complex algorithms (and is still not perfect).

Problem with TdxComponentPrinter (Delphi)

TdxComponentPrinter is a componenet that allow me print a TPanel on a paper. But there is problem and when the panel contains TImage, the background of the image will be converted to black in the paper.
Before print :
After print (on the paper) :
I have tried different ways to solve this problem. for example i used Repaint; method of TPanel,...
but doesn't work and the background of image is still black. I don't know why!!!
Help me please.
This is a png file, which might not be supported completely in your Delphi version. Did you try to change it to a TcxImage which might be more compatible with the DevExpress printing system?
It also might be worth it to submit this issue to DevExpress if you can't fix it. They reply really quick and know more about their components then all of use here on SO :-)
If it is already a jpg then you should improve the quality of the jpg. It looks like the black parts are compression artifacts where there is a mix between white and another color. If you clean up the background of the image with a paint program it will probably look a lot better.

How I can detect the background of an image programmatically?

I need to develop a software that selects a face from a photo where the background is a plain color (green, like in the movies).
Then we want to compose that selection with another background image, this part is easy with many libraries. But I don't know how can I do the selection? Can you give some links or libraries to investigate? I can do this project with any language of my choose, so examples or links in any language are welcome.
Ok, what you are trying to do is called chroma key. Like you say, it used a lot in the movies with a blue/green screen. On windows its actually pretty easy to do because its built into windows as part of GDI+ (or on C#, I think its just called the Graphics class).
I dont have any sample code handy, but the process is pretty straight forward:
With GDI+, you create a bitmap object of your foreground image (the one with the green background). Then create an ImageAttributes object. Use ImageAttribute's object's SetColorKey() method to specify a color or range of colors to use as the background color. Lastly, draw that bitmap object over the target bitmap, and GDI+ will draw it as if the background color is transparent.
There's more to it in that in code, but concept-wise thats all there is to it.
This is probably an area where it is easier to work in some other space than RGB - such as HSV.
I would also look at the OpenCV library.

Resources