# Using ROOT 5.32 standalone with VS2008 and saving canvas as image

**URL:** <https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548>\
**Category:** Newbie\
**Created:** [June 12, 2019, 7:43am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548 "2019-06-12T07:43:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 7:43am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/1 "2019-06-12T07:43:43Z")

</div>

Hello,  
I am trying to save a canvas with a plot as image / svg, but with no success so far (always empty). I have compiled ROOT 5.32 with vs2008 (we can’t change the compiler for now) and successfully were able to run example code which uses text output only. Note that I don’t use a TApplication/TRInt instance, since we already have a GUI. But when I want to save plots as image I always get a blank white image. The code is as follows:

```auto
//Needed for plugin manager
TASImage dummy;
TImageDump dummy2;

gPluginMgr->AddHandler("TVirtualPS", "svg", "TSVG",	"Postscript", "TSVG()");
gPluginMgr->AddHandler("TImage", "*", "TASImage", "ASImage", "TASImage()");
gPluginMgr->AddHandler("TVirtualPS", "image", "TImageDump", "Postscript", "TImageDump()");

TCanvas *canvas = new TCanvas();
TF1 *plot = new TF1("plot", "sin(x)", 0, 6);		
plot->Draw();
canvas->Print("test.png");	
delete plot;
delete canvas;

```

I appreciate any help you can give me. If there is a way to display graphs etc. in a second window besides our GUI this would also be very helpful.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 8:19am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/2 "2019-06-12T08:19:33Z")

</div>

> [@JMed](#):
>
> Note that I don’t use a TApplication/TRInt instance, since we already have a GUI

This is unrelated. You need a TApplication instance to process the ROOT events. And I don’t understand why you need the `gPluginMgr` related code…

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 8:28am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/3 "2019-06-12T08:28:48Z")

</div>

This is from a bugreport: [Link](https://sft.its.cern.ch/jira/browse/ROOT-2306?focusedCommentId=18508&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-18508). I had the same error while testing histogram fitting. I merely applied the solution to the image saving. I use dynamic linking, but this should make no difference.

My impression so far is, that an TApplication instance is created from the root main routine on startup. Calling gSystem-\>ProcessEvents() has no effect.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 8:33am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/4 "2019-06-12T08:33:04Z")

</div>

> [@JMed](#):
>
> This is from a bugreport: [Link](https://sft.its.cern.ch/jira/browse/ROOT-2306?focusedCommentId=18508&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-18508). I had the same error while testing histogram fitting. I merely applied the solution to the image saving. I use dynamic linking, but this should make no difference.

Well, OK, I can’t try anyway…

> [@JMed](#):
>
> My impression so far is, that an TApplication instance is created from the root main routine on startup.

which main routine? Do you start root.exe, or do you create your own standalone application?

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 8:35am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/5 "2019-06-12T08:35:26Z")

</div>

The one from the libCore.dll entry point. I don’t use root.exe. I link the dlls into our application dynamically.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 8:37am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/6 "2019-06-12T08:37:01Z")

</div>

> [@JMed](#):
>
> I link the dlls into our application dynamically.

Then you need a TApplication instance…

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 8:48am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/7 "2019-06-12T08:48:35Z")

</div>

I already tried

```auto
void ThreadFunctionRootGUI()
{
	TApplication *theApp = new TApplication("App", 0,0);
	theApp->Run();
	delete theApp;
}

```

as a separate thread, but all I get is

```auto
Error in <TApplication::TApplication>: only one instance of TApplication allowed

```

and yes, I checked with a breakpoint, it only gets called once. This is why I wrote the main entry point from libCore.dll already creates it.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 8:54am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/8 "2019-06-12T08:54:08Z")

</div>

Don’t create a separate thread, but add it in your `main()` like for example:

```auto
int main(int argc, char **argv)
{
   TApplication theApp("App", &argc, argv);

   /// Your code here

   theApp.Run();
   return 0;
}

```

see also the many examples in the `$(ROOTSYS)/test` directory

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 8:55am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/9 "2019-06-12T08:55:32Z")

</div>

If I do this, our main loop will never be executed. Our whole application won’t work.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 9:00am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/10 "2019-06-12T09:00:21Z")

</div>

Why? Do you have an infinite loop in your code? Did you try to add a `gSystem->ProcessEvents()` in your loop? Something like:

```auto
int main(int argc, char **argv)
{
   TApplication theApp("App", &argc, argv);

   while(1) {
      /// Your code here

      gSystem->ProcessEvents()
   }

   return 0;
}

```

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 9:08am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/11 "2019-06-12T09:08:56Z")

</div>

I can try this and see what happens.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 9:10am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/12 "2019-06-12T09:10:29Z")

</div>

Then I’m puzzled… And trying to fix your code by guessing what you are doing is a bit complicated…

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 9:11am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/13 "2019-06-12T09:11:51Z")

</div>

Sry didn’t see your edit, I am trying it the other way around but doubt it works.

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 9:48am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/14 "2019-06-12T09:48:45Z")

</div>

It does not make a difference whether I create the TApplication instance in the main routine from our application or not. Calling gSystem-\>ProcessEvents() in our main loop also has no effect. I am trying to dig further, but my suspicion is, that in the batch mode (it automatically is enabled) all the env variables are missing (I checked that, hence the gPluginMgr stuff) and thus probably besides other things all the fonts can’t be loaded. Sadly I could not find any documentation for this use case. This would make things much easier. I also tried setting windows ROOTSYS environment variable before posting here with no effect. The root.exe itself runs flawlessly and has env variables.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 10:02am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/15 "2019-06-12T10:02:06Z")

</div>

Which GUI are you using? How (where) did you set `ROOTSYS`? Did you also add `%ROOTSYS%\bin` in your `PATH`?

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 10:19am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/16 "2019-06-12T10:19:23Z")

</div>

GUI Is is mostly custom drawn GDI stuff and windows controls. I tried setting the path with no effect. I set the environment vars via VS and verify it in the application with e.g. getenv(“PATH”). Basically it is like Lorenzo Moneta wrote in the bug report: “When using a static library, you don’t have ROOTSYS defined”.

```auto
gEnv->Print();
TString rootsys = gEnv->GetValue("ROOTSYS", (char*)0);

```

always is empty, despite getenv(“ROOTSYS”) returns the correct path. I checked that yesterday before I started posting here.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 10:24am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/17 "2019-06-12T10:24:59Z")

</div>

OK, I’m developing on Windows since many years, and I have no idea what could be wrong in your application. And as previously said, trying to sole your issue by guessing what you are doing is a bit complicated… Note also that ROOT v5.32 is quite old…

> [@JMed](#):
>
> Basically it is like Lorenzo Moneta wrote in the bug report: “When using a static library, you don’t have ROOTSYS defined”.

This is irrelevant. Are you sure to use static libraries? Did you build ROOT yourself?

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 10:27am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/18 "2019-06-12T10:27:17Z")

</div>

I will try to create a new empty project and see what happens. If that works I can compare the differences.  
I use the old 5.32 version, because 6.xx does not support vs2008 and I can’t change the compiler.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 12, 2019, 10:28am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/19 "2019-06-12T10:28:30Z")

</div>

> [@JMed](#):
>
> Basically it is like Lorenzo Moneta wrote in the bug report: “When using a static library, you don’t have ROOTSYS defined”.

This is irrelevant. Are you sure to use static libraries? Did you build ROOT yourself?

---

<div class="post-metadata">

**Author:** ![JMed](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@JMed](https://root-forum.cern.ch/u/JMed)\
**Post date:** [June 12, 2019, 10:43am UTC](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548/20 "2019-06-12T10:43:01Z")

</div>

No, as I wrote in my posts, I link them dynamically, but as I wrote in my second post, this should not make any difference (static or dynamic). Also I wrote in my first post, that I build it myself.

[Next page](https://root-forum.cern.ch/t/using-root-5-32-standalone-with-vs2008-and-saving-canvas-as-image/34548.md?page=2)
