DrawPicture poor graphics scaling quality on Windows

A picture speaks a thousand words:

Here’s the code that does the scaling (this is in a DesktopCanvas’s Open event):

dim p as Picture = Picture.FromData(DecodeBase64(ICON))
dim p2 as new Picture(Me.Width, Me.Height)
p2.Graphics.DrawPicture(p, 0, 0, p2.Width, p2.Height, 0, 0, p.Width, p.Height)
Canvas1.Backdrop = p2 

My image is several times larger than the canvas and is being reduced to fit.

Is there a way to do this so the scaling doesn’t look so awful on Windows?

Solved by adding the following line before DrawPicture:

p2.graphics.AntiAliasMode = graphics.AntiAliasModes.HighQuality

Imho that should really be the default mode!

5 Likes

It depends on which version of Xojo you are using. I used this technique with Xojo 2019:

Public Sub SetGDIInterpolationMode(g as Graphics)
  #if TargetWin32
    // sets high quality interpolation mode for resizing info
    // useful when using @2x retina graphics on Win32 
    //
    // Note: you set this on the destination graphics, not on the source
    
    // see https://forum.xojo.com/9403-scale-quality-of-canvas-control/0
    if g = nil then
      return
    end if
    
    dim ghnd as integer = g.Handle(Graphics.HandleTypeGDIPlusGraphics)
    
    // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms534141(v=vs.85).aspx
    
    Soft Declare Function GdipSetInterpolationMode Lib kLibGDIPlus (graphicsHandle as int32, Mode as int32) As int32
    
    const InterpolationModeDefault = 0
    const InterpolationModeLowQuality = 1
    const InterpolationModeHighQuality = 2
    const InterpolationModeBilinear = 3
    const InterpolationModeBicubic = 4
    const InterpolationModeNearestNeighbor = 5
    const InterpolationModeHighQualityBilinear = 6
    const InterpolationModeHighQualityBicubic = 7
    
    dim mode as integer = InterpolationModeHighQualityBicubic  // this looks good but you might see subtle box-edge effects if the image has an Alpha channel
   
    call GdipSetInterpolationMode(ghnd,mode)
    
  #endif
  
End Sub
1 Like

Thanks Mike,

I had previously used that in api 1 as well, but now there is no longer a Graphics.HandleTypeGDIPlusGraphic.

Still good code to keep around though for older Xojo versions.