Window.BitmapForCaching

From Xojo Documentation

Method

Window.BitmapForCaching(width As Integer, height As Integer) As Picture

New in 2016r1

Supported for all project types and targets.

Returns a bitmap that is configured correctly for using as a cache for content to be drawn to this Window. This image supports Alpha Channels (not masked images).

Notes

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

Raises exceptions in the following cases:

  • InvalidArgumentException if width, height, or scale are less than or equal to zero
  • OutOfMemoryException if the picture could not be allocated

Examples

If you need to support images with masks AND images that use alpha channels code like the following will let you handle both cases for HiDPI displays

Public Function BitmapForCaching(Extends g As Graphics, width As Integer = -1, height As Integer = -1, withMask As Boolean = False) As Picture
#Pragma BackgroundTasks False
If width = -1 Then width = g.Width
If height = -1 Then height = g.Height

If width <= 0 Then width = 1
If height <= 0 Then height = 1

#If TargetLinux Then
If withMask Then
Return New Picture(width, height, 32)
Else
Return New Picture(width, height)
End If
#Else
Var pic As Picture
If withMask Then
pic = New Picture(width * g.ScaleX, height * g.ScaleY, 32)
Else
pic = New Picture(width * g.ScaleX, height * g.ScaleY)
End If
// Set the resolutions
pic.HorizontalResolution = 72 * g.ScaleX
pic.VerticalResolution = 72 * g.ScaleY
pic.Graphics.ClearRect(0, 0, pic.Width, pic.Height)

// Make sure the two graphics object scales match the reference graphics object
Var gScaleX As Double = g.ScaleX
Var gScaleY As Double = g.ScaleY
pic.Graphics.ScaleX = gScaleX
pic.Graphics.ScaleY = gScaleY
If withMask Then
pic.Mask.Graphics.ScaleX = gScaleX
pic.Mask.Graphics.ScaleY = gScaleY
End If
Return pic
#Endif
End Function