Problems saving canvas to PNG

Hello,

Just learning the graphics system, and having fits with what SEEMS to be a simple process…

My original program draws a hex grid. In the future, it would walk a path across the grid, then save the results as an image. I can’t for the life of me successfully print that Canvas. I finally created a scratch project, described below.

Test form consists of a canvas object and a single button. The only user-written code is in the button, and is listed below. I tried to simplify this when I couldn’t figure out the problem saving the image.

Trying to save a canvas (named imgMap) to a PNG after drawing a simple line on the canvas. The line itself is visible on the Canvas displayed on the form, so I know MOST of what is listed is correct.

PROBLEM: PNG file is successfully created, but is blank when opened - the line is not there. My [admittedly very simple] code follows.

===
dim oFolder as FolderItem
Dim pic As New Picture(125, 125, 24) ’ original canvas is 125 x 125 as well

oFolder = SpecialFolder.userhome.child(“Traverse”).Child(“test.png”)

frmMap.imgMap.graphics.drawline(0,0,100,100)

’ *** IS THERE A MISSING STEP HERE??? ***

frmMap.imgMap.DrawInto(pic.Graphics, 0, 0)

pic.Save(oFolder, Picture.SaveAsPNG)

===

Is there something I need to do between the DrawInto and save step???

Many thanks, this is driving me batty.

The contents of a CANVAS (which I assume frmMAP.imgMap is) should never be accesses outside of its PAINT event

What I would do in this case… is NOT draw to the Canvas, but draw to the Picture (pic) and then paste THAT into the Canvas using DrawPicture

// global method so this is PUBLIC
Dim pic As New Picture(125, 125) ' original canvas is 125 x 125 as well [forget about DEPTH]
//BUTTON ACTION
dim oFolder as FolderItem
oFolder = SpecialFolder.userhome.child("Traverse").Child("test.png")
pic.graphics.drawline(0,0,100,100)
pic.Save(oFolder, Picture.SaveAsPNG)
frmMap.imgMap.invalidate
//PAINT EVENT of frmMap.imgMap
g.drawpicture pic,0,0

Was able to create a local pic and get it to save to image.

===

dim oFolder as FolderItem
dim pic as new picture(1000,1000)

oFolder = SpecialFolder.userhome.child(“Traverse”).Child(“test.png”)

pic.graphics.drawline(0,0,100,100)

’ copy the picture to a canvas on my form; it does show the line I drew above
frmMap.imgMap.Backdrop = pic

’ This works with local pic - it does save the image to PNG file with the line now visible
pic.Save(oFolder, Picture.SaveAsPNG)

===

Tried to create a global pic for use throughout the app, and no luck. Can’t get it to work. Another issue is that I want to resize the picture, based on a size selected by the user.

Got more work to do…

so what did you try?

Create a MODULE, define the picture as a public variable

I have app level methood called Create_pic, scope is public
code: dim mypic as new picture(1000,1000)

in app.open I call create_pic

then in button code

app.mypic.graphics.drawline(0,0,100,100)

I get error that the item does not exist. If I try app.mypic… it says app has no member named myopic

That is because mypic is only within the scope of APP

What I said to do was create a new MODULE, and create the myPic varialbe there… not in a method

App.mypic does not exist… because you created myPic inside a Method named CREATE, so CREATE ‘owns’ it

OK…ty for interactive responses :slight_smile: I appreciate your patience while I try and understand all this

So, created MODULE called GLOBALS

to that Module added the method that DIMS the pic

Create_pic(intWidth,intHeight)
dim mypic as new picture(intWidth,intHeight)

also added a property called mypic; that doesn’t seem to work…

I still can’t wrap the brain around it. If you can walk thru the steps after adding the MODULE I would greatly appreciate it.

It seems like that METHOD works, but cannot figure out how to use that global.

Too many years since Visual Basic 6 .

No…no, no and once again no
By doing that you once again limit the scope of myPic to where you “dim’d” it

download this, perhaps it might help explain it

www.rdS.com/ed_anderson.xojo_xml_project

BINGO! Now I got it, many thanks!