Picture.Graphics is Nil

Hi all,

the code below works fine excepted Graphics is Nil:

OffScreen = Picture.Open(img_FI)
If OffScreen <> Nil Then
// OffScreen.Graphics is Nil here.

Why ?

Because it’s loaded from a file, thus immutable. See this relevant note in the documentation. You’ll need to draw it into a new picture object to make changes.

3 Likes

Thank you Anthony.

So, I need to create another MyPicture(w,h,32) to get a valid .Graphics, then draw the one I load from disk.

At last, I can Draw above the artwork what I want using MyPicture.Graphics…

Not very intuitive, IMHO.

BTW: the DesktopCanvas does not have a .Graphics (none visible in the debugger). But there is a Backdrop.

The Graphics is only available in the paint event. That’s the only place you can do the drawing for a DesktopCanvas (or even the old Canvas).

1 Like

Close but not quite: the (width, height,depth) constructor is I believe from API 1 and you don’t use that any longer, just

= new Picture(width, height)

I rather think the constructor with 3 parameters creates a picture with a mask, while the one with 2 parameters is for an alpha picture, regardless of API1 or API2.

5 Likes

Gemini gaves me this code:



// 1. Load the image from disk
Var f As FolderItem = SpecialFolder.Desktop.Child("Green.png")
Var originalPic As Picture = Picture.Open(f)

If originalPic <> Nil Then
  // 2. Create a new picture of the same size to act as your canvas
  Var canvasPic As New Picture(originalPic.Width, originalPic.Height)
  
  // 3. Get the graphics context
  Var g As Graphics = canvasPic.Graphics
  
  // 4. Draw the original image first
  g.DrawPicture(originalPic, 0, 0)
  
  // 5. Draw your shapes or text on top
  g.DrawingColor = Color.Red
  g.PenSize = 5
  g.FillRectangle(20, 20, 100, 50) // My addition…
  g.DrawText("Hello Xojo!", 25, 45)
  
  // Now you can display canvasPic in an ImageWell or Canvas
  cPreview.Backdrop = canvasPic
End If

I changed the file name and the cPreview Canvas…

I love the typing economizer (Var g As Graphics = canvasPic.Graphics); yes Ian, this works fine !

This is misleading because I was not talking about the Graphics from the Paint Event…

Ian, Michael, read the code snipped I questioned, reproduced below:

This Graphics IS NOT IN A PAINT EVENT.

i believe Ian was responding to this text and does not deserve to be shouted at.

2 Likes

Actually you asked about not being able to see a Graphics object for the DesktopCanvas, I replied to say that the Graphics Object i only available in the Paint event of the DesktopCanvas, you cannot gain access to it in any other way. Which seems pretty straight forward and not misleading in any way. There is no graphics object associated with a DesktopCanvas outside of the paint event.

1 Like

Nore is it anything to do with a DesktopCanvas. Please do not shout.

1 Like

Emile,

please read this:

Updating code that used the Graphics property

and you might understand.