TCanvas background image

I need to draw an image (.gif) as the background ot a Canvas.
Perhaps it is a trivial thing to do, but please anyone send me the code lines for that. Thanks

see example below

Rene

[code]void canvasimage() {
//create a canvas with a picture (.png,.jpg,.gif, etc) in the background

TImage *img = TImage::Open(“rose512.jpg”); //put your own picture here
TCanvas *c1 = new TCanvas(“c1”);
img->Draw(“x”);

//Create a transparent pad filling the full canvas
TPad *p = new TPad(“p”,“p”,0,0,1,1);
p->SetFillStyle(4000);
p->SetFrameFillStyle(4000);
p->Draw();
p->cd();
TH1F *h = new TH1F(“h”,“test”,100,-3,3);
h->SetFillColor(kCyan);
h->FillRandom(“gaus”,5000);
h->Draw();
}
[/code]

Thank you very much Brun

tutorials/image/trans_graph.C migh help

I just would like to give some feedback with some question marks:

It seems that one will have always a pixel based result and no vector graphic over the background image when storing the image, isn’t it so?

Your example, Rene does exactly what I would need to compare a result to an old published one in a scanned paper. Overlaying works without problems. But storing this picture as an pdf gives no background at all and storing as an eps works but it is rendered (also the graph). Thus the result won’t give satisfactory figures to include into a thesis (lines and text that is not vector based will appear always not sharp if the picture is scaled in the latex file).

Valerie’s example is also manipulating pixels. Thus the result is also only rendered. Is it very complicated to teach the pdf creator of root to handle the background of a pad as an image but the graph itself as an vector object?

A third attempt by me was a several pass one:

Store the graphs as pdf
create the background by extracting the pictures from the paper separately into gimp and store it as an eps
eps2pdf
pdftk rootoutput.pdf background gimpbackground.pdf output merged.pdf

Unfortunately it seems that root prints a white background behind the graph and thus the background is not visible. But maybe my approaches are simply wrong and there is another solution I do not know about?!

The easiest will most probably be to extract the data points from the old publication by eye…

TImage::Open accepts PS file.
Try to modify $ROOTSYS/tutorials/image/img2pad.C by replacing one of the “rose512.jpg” file with one of your PS file.

Hi!

Which command do I have to use in order to save the output graph?
I usually use:

	string output = "something.svg";
	c->SaveAs(output.c_str());

But I noticed that such svg file doesn’t show the background image.
The same happen if I create a pdf file.
I get the background if I create an eps file…

How to save it as a pdf?

Thank you!

string output = "something.png";

I need it to be a vectorial image.

The background image is bitmap anyway.

try ps … may be…

string output = "something.ps";

Uhm… So there’s no way to directly get a pdf?

no. PS might work though. But as I said the background is bitmap anyway

1 Like

I think I stick to eps.
I still have to try ps, but I can’t directly view ps file on my distro. I have to convert them to pdf first… :\

eps format is ok. It will just take some more time to convert them to pdf when loaded in Latex.

Thank you couet! :slight_smile: