How to use a thread in a long running crop method in a Canvas Object


I’m using a long running crop method from a Canvas Object. I learned this out of Eugene Dakins Canvas Book Ex 8-2. Below. If it’s small crop #Pragma DisableBackgroundTasks work fine. In my project I am doing a large crop where I’m getting a not responding message and my crop window jumps behind my main window. This creates a poor user experience bug that have to fix.

Paul Levfebvere did a Xojo Youtube training video on threading back in 2018 that has the exact solution to my problem to fix my bug. This uses a Thread, Timer & Progress Bar. The rule the long running code must run in the thread event and must not touch any part of the user interface. I run my long running crop code from a button event on my crop picture window. In Pauls first threading example (which I can’t find in the old hubwiz docs).

He takes the code from the button event and put’s it in the Thread event. I thought oh that should be easy. I put my slow long running code in the Thread event. Well my canvas object method runs from a Canvas that is on the window interface. Throws an Error. That means I have the run the code below from the Canvas Object. I Jumped in and tried to figure it out from there - confussion??!! I’m working this from Eugenes code example to keep it simple. How do I tackle this were the thread event doesn’t choke on it?


Preformatted text`Crop(X as Integer, Y as Integer, Width as Integer, Height as Integer, Pic as Picture)

#Pragma DisableBackgroundTasks
//Crop the picture
//Create a new picture with width and height
Dim NewPic as Picture
NewPic = new Picture(Width,Height)

Dim w,h as Integer
//Copy pixels from old picture to new picture
For w = 0 to Width //Go through each row
  For h = 0 to Height //Go through each column
    NewPic.RGBSurface.Pixel(w,h) = Pic.RGBSurface.Pixel(w+X,h+Y) 'Copy Pixels
  Next h
Next w
//Return the cropped picture
Return NewPic

I’m sidestepping your question a bit, but why don’t you simply use the extra parameters in Graphics.DrawPicture to do the cropping? I’m certain it would be faster than this method.

1 Like

neupic.Graphics.drawpicture pic,0,0,width,height,x,y,width,height

I wrote this code when the book material was all fresh in my head about 3 3/4 year ago. In my original project code I did use the DrawPicture method with the crop Method nested in it. So your implying I don’t even need that loopy slow crop code to get the same result with DrawPicture and I don’t even need to bother with the Thread. So the book was just giving me a different obscure bulky way to get the same result of DrawPicture. But I think this concept was built on using a Canvas Graphics Buffer. Eugene Drops in here in here once in a while. Maybe he can shine some light on this of why use this crop method code. Anyway thanks for your input. Somebody right after you said the same thing.

Crop(X,Y, W, H, NewImg)
DrawPicture(myImage, X, Y, W, H)

DrawPicture(OrigImg.Crop( X,Y, W, H, NewImg), X, Y, W, H)

cvsCapturPic.PBuffer.Graphics.DrawPicture(cvsCapturPic.Crop(frmGAmain.CropX, -27 * frmGAmain.RatioH, frmGAmain.CropW + BdrPen, 214 + BdrPen, frmGAmain.cvsGtr6FrtBrd.PBuffer), 34, 0) ’

That’s the downside of having to manufacture a problem to illustrate some technique. They don’t always make real-world sense.

This book did simplify a lot of Canvas OOP coding problems that I couldn’t ever get my head around. Now I have simplify something that was made so complex. two sets of X, Y, H, W Image properties to one set to get the same result. Right in front of my nose. Thanks Guys for your Help Happy New Years To You.