Create image of a canvas in memory

Hi,

I have a canvas and I would like to have a jpg/gif/png image of its content and save it in a database. I see that I can use SaveAs to save an image corresponding to a canvas into a file.
How can I do the same, but not create a file simply get a pointer to the actual image data ?
Something that can be saved in a database and retrieved by a non-ROOT process and displayed (for example PHP) ?

Thanks in advance for your help,
Barth

see $ROOTSYS/tutorials/image/*

Rene

Thanks for the pointer.

Best regards,
Barth

Hi again,

I went through the tutorials, but I don’t think any of them answer my question.

I will try to rephrase my problem :

I have a TObject. If I would Draw() this TObject on a Canvas and then SaveAs(“xxx.png”) this Canvas, I would get a file containing a PNG image. Right ?
Now, all I want is the content of the file to be saved in a database. However, I don’t want to display anything (there is no gui) nor to save locally a file.

How do I achieve this ?

Thanks a lot for your help
Barth

Hi Barth,

What’s wrong with this tutorial:[code]void pad2png()
{
// Create a canvas and save as png.
//Author: Valeriy Onuchin

TCanvas *c = new TCanvas;
TH1F *h = new TH1F(“gaus”, “gaus”, 100, -5, 5);
h->FillRandom(“gaus”, 10000);
h->Draw();

gSystem->ProcessEvents();

TImage *img = TImage::Create();

//img->FromPad(c, 10, 10, 300, 200);
img->FromPad©;

img->WriteImage(“canvas.png”);

delete h;
delete c;
delete img;
}[/code]
Just don’t write the image to file… Then you can access the image buffer (see TImage doc: http://root.cern.ch/root/html/TImage.html)

Cheers,
Bertrand.

Hi Bertrand,

I saw this tutorial and thought that I couldn’t access the image buffer directly. Although I read the documentation of TImage, I missed the GetImageBuffer method…
Sorry, about that and a big thank to all for your help.

Barth

With QtRoot plugin one can use
root.bnl.gov/QtRoot/htmldoc/TEmbeddedPad.html class also

Hello !

I am wondering why TEmbeddedPad is in QtRoot and not directly in ROOT ?

I see that TPad could even be TCanvas-less by adding a Constructor as stated by Valeri here : root.cern.ch/phpBB2/viewtopic.php?t=3185

Therefore, wouldn’t it be nice to have this feature (tpad canvas-less) directly in root ?

Cheers,
Barth

No plans to add a TCanvas-less TPad, this would just add confusion and it is not required. See previous mails above to get access to the image pixels info.

Rene

[quote=“brun”]No plans to add a TCanvas-less TPad,[/quote]It is already exists. [quote=“brun”]this would just add confusion and it is not required.[/quote]One doesn’t need to create any separate class. It is enough to add some option to TPad ctor indeed. If such flag would be there one might not have introduced the TEmbeddedPad at all to satisfy some users’ needs. I had no idea how to do what I needed via TCanvas/TPad combination.[quote=“brun”]See previous mails above to get access to the image pixels info.[/quote]It is not a main question. The main question is, “How to create TPad image off-screen within the interactive ROOT session?” The TEmbeddedPad class is to address very this issue.[quote=“Barth”]I am wondering why TEmbeddedPad is in QtRoot and not directly in ROOT ? [/quote]3 years ago root.cern.ch/phpBB2/viewtopic.php?t=3185 I was wondering too. TEmbeddedPad has no Qt package dependency. I had no time to argue though. It was faster and simpler to write that class meantime and move on