Create a muttable picture from an Immutable one

I’m trying to load a picture from a file and then change it from an immutable object to a mutable picture that I can draw on. Does anybody have any examples.

DrawPicture it into a new picture of the same dimensions?

Julian, this is the code:

On a mac, big sur. What I’m trying to do is load the picture and then draw on it. When hi_dpi is off it works. With hi_dpi on the image is about 1/3 of the original. Is there is way to get the image to fill


// in the canvas open event
dim p as new Picture(ExampleFile.Width,ExampleFile.Height) 
p = ExampleFile

//Add the ExampleFile to the Picture

PBuffer= self.BitmapForCaching(ExampleFile.Width,ExampleFile.Height) // should set the scaling
dim g as Graphics = PBuffer.Graphics

//Draw an outer border to the picture
pbuffer.graphics.drawpicture(p,0,0)
PBuffer.Graphics.DrawRect(0,0,Canvas1.Width, Canvas1.Height)  //works
PBuffer.Graphics.DrawLine(0,0,100,100) 'Draw the graphics line   //works

// in the paint event of the canvas I draw the picture
If PBuffer <> Nil then
  //Draw the picture to the graphics layer
  g.DrawPicture(PBuffer,0,0)
End If

Pbuffer is a picture property of the window
hi_dpi on:

Screen Shot 2021-11-27 at 4.29.01 PM

ExampleFile is a png file dragged into the navigator. It is not a high resolution image. But it looks fine when hi_dpi is turned off.

Screen Shot 2021-11-27 at 4.29.40 PM

See Window.BitmapForCaching - Xojo Documentation (old) Or DesktopWindow.BitmapForCaching - Xojo Documentation (new)

Use this method instead of "New Picture" in order to get a Picture image that is suitable for HiDPI displays.

Just out of interest, is there a particular reason you’re asking this here instead of on a forum filled with xojo users and answers to this question?

I was going off of Greg’s 2016 blog. I’ll try again when I get back to work.

I used this blog because Norman wrote a “Better Bitmap For Caching” method.

BitmapForCaching

Is generally used to create “pixel perfect” bitmaps to cache various elements or custom controls for onscreen use only. It will give you different sized images depending on the OS, connected screen that the window is displayed upon and even OS Version. “pixel perfect” bitmaps reduce the need for the OS to scale the image and therefore save processing cycles at draw time (which you WANT to be as fast as possible).

If you want to do general image manipulation, you should follow the suggestion of Julia, and that is to create a new image based upon the old image’s dimensions, draw the old image into the new image and then perform your translations.

Thanks Sam. I’m going to do that.

Henry

1 Like