How can I save GDI Canvas to a file?

0 votes
asked Feb 20, 2018 by synack85 (150 points)

I'm generating stimulus with 256 black and white squares (40x40px) - So 16 squares per row

I added a GDI Canvas (640x640) and a roulette and add this to 'beforeonset'

int row=0;
int col=0;
for (int i=0;i<256;i++)
	if (block[i]==1){ //block roulette
		GraphicsClass.FillRectangle(new SolidBrush(Color.White), (col*40),(row*40),(col*40)+40,(row*40)+40);		
			GraphicsClass.FillRectangle(new SolidBrush(Color.Black), (col*40),(row*40),(col*40)+40,(row*40)+40);		
	if (col==16){


then I added 


in Before Offset 

Now I want to save to a JPG file each stimulus that I generate in graphics class.

Can anyone help me ?


1 Answer

0 votes
answered Feb 20, 2018 by Ilia Korjoukov (13,720 points)
selected Feb 20, 2018 by qandaadmin
Best answer


There is no an easy way to save the generated canvas image to a file directly. In theory the Graphics class in GDI+ allows copying graphics to a bitmap, which, in turn, can be saved into a file. In practice, however, it did not work last time I tried it.

There is another easy way to save a screenshot of the entire screen (at any runtime moment) with a dedicated command. In order to do it:

  1. Go to the Experiment ribbon tab.
  2. In the Properties panel find the 'Save Screenshot Now' property and create a proxy variable bound to it.
  3. Use the following code in a snippet that is invoked right after the updated canvas is rendered on the screen:


SaveScreenshotNow=@"C:\MyData\Stimuli_"+StimulusNumber+".jpg"; /* StimulusNumber represents some stimulus id.*/


A couple of remarks on the above procedure:

You should not use SaveScreenshotNow in the same snippet, in which GraphicsClass does rendering procedures. Instead, you have to wait until the new rendering is presented on the screen, which typically happens automatically after the 'rendering' snippet call. Thus, the best place for the SaveScreenshotNow command is the next snippet in a timeline, given that the target event (one containing the GDI Canvas element) is active. If you do rendering in advance, then wait until the onset of the target event to make a screenshot.

In the newest version of EventIDE, you need to apply rendering updates on the GDI Canvas Element manually, by calling 'UpdateCanvasNow=true;', right after the rendering procedures. The 'Update Canvas Now' is a new command property of the GDI canvas element.

I hope you find the above useful. Please contact me directly, if you need more details or any help.








Welcome to EventIDE Q&A forum where you can ask questions about EventIDE software and receive answers from other members of the community

FAQ questions

Installation and License



EEG Analysis

Visual Stimuli

Runtime and Data Collection