From Xojo Documentation

Revision as of 19:07, 23 July 2021 by Gperlman (talk | contribs)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

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

New in 2021r3

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).


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


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)
Return New Picture(width, height)
End If
Var pic As Picture
If withMask Then
pic = New Picture(width * g.ScaleX, height * g.ScaleY, 32)
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
End Function