creating a pic by cropping another pic

My small app needs a 256x256 picture. I would like to allow my users to add a pic of any size and proportion and have my app handle resizing it. If a pic is larger and not a square, I want to crop the longer side, then resize the remaining square to 256x256. I have it working that a user can drop any pic on my canvas, and, using…

g.DrawPicture myDropPic, 0, 0, me.Width, me.Height, 0, 0, myPicWidth, myPicHeight

where myPicWidth and myPic Height are = to each other, display the image as I wish it to be in the canvas. When I go to save “myDropPic” as a png, of course, and recover it, I then need more massaging to make it work.
It would be much better if I could create a NEW pic from the dropped one that was cropped and resized correctly. I have tried using graphics.crop, drawing to the graphics of a new pic, etc. and can’t seem to find the solution I need. Help much appreciated.

I would avoid automatic cropping. You never know where the important part of an image is. The user will never be satisfied.

It is probably wiser to resize it and place it at the center of the square.

Something like what I posted there could suffice for that https://forum.xojo.com/12211-proportionally-resizing-a-picture/0

Thank you, Michel. That is good advice for displaying the pic in a canvas. But I still would like to create a new pic of the cropped and resized image. Any idea how to do that?

I don’t usually plug my paid offerings, but when they’re relevant, they’re relevant. So in the interest in offering a solution, you might consider http://thezaz.com/code/zirconcropper/

Demo DL’ed, Thom. Will give it a try

Some example code I use on a super simple color inversion app (pPictOriginal and pPictReversed are defined as properties). Probably not great, but it serves a purpose.

[code]//open picture from file
Dim f As FolderItem = GetOpenFolderItem(FileTypes1.All)
If f Is Nil Then
Return
Else
Dim p As Picture
p = f.OpenAsVectorPicture
mCreateOriginalPict§
mInvertColor§
End If

======================================================

Protected Sub mCreateOriginalPict(p As Picture)
// Store Original Picture in Memory //
pPictOriginal = New Picture(p.Width, p.Height, 32)
pPictOriginal.Graphics.DrawPicture(p, 0, 0)
End Sub

======================================================

Protected Sub mInvertColor(p As Picture)
// Invert Picture Colors //
pPictReversed = New Picture(p.Width, p.Height, 32)
pPictReversed.Graphics.DrawPicture(p, 0, 0, p.Width, p.Height)
Const kMaxMapOffset = 255
Dim map(kMaxMapOffset) As Integer
For i As Integer = 0 To kMaxMapOffset
map(i) = kMaxMapOffset - i
Next
pPictReversed.RGBSurface.Transform(map)
End Sub[/code]

That’s the line I was not finding on my own. Thanks, Langue.