Creating HiDPI picture in code

I’m trying to move some of my code over from previous image creation code easily to the new HiDPI. I’ve got a function that theoretically SHOULD produce a 100x100@144ppi image that I can draw onto, but it’s not working quite right:

[code] dim ppx as Picture = new Picture(100,100)
dim i as double = winmain.ScaleFactor //this returns 2 when on a retina machine

if i<>1 then
  ppx.HorizontalResolution = 72 * i
  ppx.VerticalResolution = 72 * i
  
  ppx.Graphics.ScaleX = i
  ppx.Graphics.ScaleY = i
end if

Return ppx[/code]

The problem is that when I change the scaling factor of the ppx.graphics, it actually reduces the height & width of the graphics. So the picture itself has a height and width of 100x100@144ppi, but the graphics object of that picture has a height and width of 50x50@2 scale. The problem now is drawing anything onto the graphics itself will produce cut off content as it only draws onto the part of the graphics in which the graphics size matches the parent picture’s size.

Is there any way to quickly and easily create a picture with the appropriate resolution and have the graphics also scale correctly?

Ok so if I slightly change it to:

[code]
dim i as double = winmain.ScaleFactor //this returns 2 when on a retina machine
dim ppx as Picture = new Picture(100i,100i)
if i<>1 then
ppx.HorizontalResolution = 72 * i
ppx.VerticalResolution = 72 * i

  ppx.Graphics.ScaleX = i
  ppx.Graphics.ScaleY = i
end if

Return ppx[/code]

Then it seems to work fine with the canvas. The only problem is that the picture then thinks it has a different height than the intended height of the actual picture. So the horizontal/vertical resolution seems to only affect what happens when the picture is displayed on a canvas.

Will probably have to make extension methods for picture.ActualWidth and picture.ActualHeight since picture width/height now are sort useless when it comes to drawing pictures in this situation.

Rather than doing this:

dim ppx As Picture = new Picture(100, 100)

Try:

dim ppx As Picture = Self.BitmapForCaching(100, 100) ' Self should be your Window

[quote=281624:@Paul Lefebvre]Try:

dim ppx As Picture = Self.BitmapForCaching(100, 100)[/quote]

… and don’t do any of the other code as the picture is properly setup :slight_smile:

ppx.Width/Height will give you the Pixel size. For Point size use ppx.Graphics.Width/Height (as long as you don’t mess with ppx.Graphics.ScaleX/Y).

Thanks. And when doing this, am I no longer allowed to modify the picture’s mask? Seems that attempting to access the created picture’s .mask value at all throws a NilObject, and I must now use applyMask for something created with .BitmapForCaching? Switched over cause a nilobject when trying to access the .mask value at all (to view or modify) even though it was a mutable picture. But using ApplyMask got around that for some reason.

I think masking is antiquated and every things alpha channels now, or maybe not. Anyways with alpha channel pictures CopyMask and ApplyMask are the ways to get and set the alpha channel.