Scroll Image Size in Canvas is showing 2 Images instead of one

Hello
Paul Lefebvre did a Xojo Teaching Demo Video 8 years ago The Clever Canvas Control. (Great Video Thanks Paul). There was a sample project called Canvas Zoom. He put code similar to the code below in the Paint Event of the Canvas Object that resized the picture in the canvas with a slider control. I used this modified code trying to get it to work in the valueChanged event of the slider control to only reduce the Image size. The Canvas Object Paint Event was already used for something else in my project.

My Problem - Instead of having the one main image to resize. The original Image stays layered behind while the resizing Image is in front giving two images. I’m using two Picture Buffer Properties PBuffer & APBuffer. The PBuffer property Container holding the original source Image was already created by another crop Method. I’m Thinking the Source Image and the target resize has to be separate for it to work.

How do I get the one Image only to resize to be able to save the resized Image to a graphic file with the resized container outline? Thank You

  cvsCapturPic.DoubleBuffer = True
  cvsCapturPic.Transparent = True
  app.UseGDIPlus = True
  
  
//Create Canvas Scale H & W Integer Properties to Change change to the Slider
  Dim scaledWidth As Integer = cvsCapturPic.Width * (sldrResize.Value/100)
  Dim scaledHeight As Integer = cvsCapturPic.Height * (sldrResize.Value/100)
  
  // Create Picture Container Outline
  cvsCapturPic.APBuffer = New Picture(cvsCapturPic.Width, cvsCapturPic.Height)
  
  cvsCapturPic.APBuffer.Graphics.DrawPicture(cvsCapturPic.PBuffer, _ // The Resized Picture
  0, 0, scaledWidth, scaledHeight, _ // scaled size
  0, 0, cvsCapturPic.Width, cvsCapturPic.Height) // Original Size
  
  If scaledWidth <= cvsCapturPic.Width Then
    lblReWidth.Text = Cstr(scaledWidth)
  End If
  If scaledHeight <= cvsCapturPic.Height Then
    lblReHeight.Text = CStr(scaledHeight)
  End If
  
  'End If
  
  cvsCapturPic.Invalidate(False)

In the canvas Paint event, you should only be drawing APBuffer and not PBuffer.

1 Like

Hi Tim - Thanks for responding. I already found one problem that I corrected. The APBuffer container doesn’t get resized. After I corrected that it definitely cleanly resized in the APBuffer in debug. In the Paint Event Handler I have it in there maybe the syntax is wrong or I have to rewrite it different.

  // Create Picture Container Outline
  cvsCapturPic.APBuffer = New Picture(scaledWidth, scaledHeight) 

Ok, I think I see what i did and have to change. Is this correct?

'Draw the picture to the graphics layer

  If APBuffer <>Nil Then
    If PBuffer <>Nil Then
      'Draw the picture to be resized - lock bottom and right canvas
      g.DrawPicture(PBuffer, 0,0,me.Width, me.Height, 0,0, PBuffer.Width, PBuffer.Height)
    End If
  Else
    If APBuffer <>Nil Then
      'Draw the picture to be resized - lock bottom and right canvas
      g.DrawPicture(APBuffer, 0,0,me.Width, me.Height, 0,0, APBuffer.Width, APBuffer.Height)
    End If
  End If
  
   g.DrawRect(0, 0, me.Width, me.Height)`Preformatted text`

I’m not entirely sure what your goal is, but why are you scaling PBuffer into APBuffer and then rescaling it when you draw it in Paint?

Oh, are you scaling it down to fit the canvas? That would make sense.

I’m crop off a main canvas to create a savable graphic file that has a labels and border. I am adding a feature to be able to resize it smaller or custom resize it. I just added another picture buffer property SzPBuffer. APBuffer is used somewhere else causing a problem. Just tested it’s still broke. I have to debug what going on. You got me in the right direction. Thanks Tim

If SzPBuffer <> Nil Then
   'Draw the picture to be resized - lock bottom and right canvas
   g.DrawPicture(SzPBuffer, 0,0,me.Width, me.Height, 0,0, SzPBuffer.Width, SzPBuffer.Height)
 Else
   If PBuffer <>Nil Then
     'Draw the picture to be resized - lock bottom and right canvas
     g.DrawPicture(PBuffer, 0,0,me.Width, me.Height, 0,0, PBuffer.Width, PBuffer.Height)
   End If
   
   If APBuffer <> Nil Then
     'Draw the picture to be resized - lock bottom and right canvas
     g.DrawPicture(APBuffer, 0,0,me.Width, me.Height, 0,0, APBuffer.Width, APBuffer.Height)
   End If
   
 End If
  g.DrawRect(0, 0, me.Width, me.Height)