Resize picture to a ImageWell

How do I resize a picture to fit in a ImageWell ? I want to drag the picture - at the same time - resize it to fit.

Any suggestion on how to do it ? If I move to a Canvas, I will be able to resize it ?

Have a look at the Graphics class :slight_smile:

As mentioned, your best (and possibly only) bet is to go after the Paint event and resize the image when you have it with graphics.drawpicture

The logic that handles the drag/drop and the paint refresh you’ll also need to take care about.

On Cocoa, you can get the OS to do it for you. Put this code in the Open event of an ImageWell.

// - (void)setImageScaling:(NSImageScaling)scaling declare sub setImageScaling lib "Cocoa" selector "setImageScaling:" ( handle as integer, value as integer ) setImageScaling( me.handle, 0 )

Hello,

I have an example for a Canvas in a Real Studio project at Click Here. Not sure if this is adaptable to an imagewell for Xojo, and it is worth a try. :slight_smile:

Sincerely,

Eugene

MBS has a wonderful routine (Picture.ScaleImageAndMaskMBS(Â…)) that can resize the image, mask out the white space and even perform anti-alias smoothing in one line! It is cross-platform too.

1 Like

How I do this ? I want to drop a picture to a canvas and refresh it. When I drop, I will resize the image.

I am putting the below in the Canvas DropObj picture, but I am getting an issue when I resize it.

I have mImage defined as a Property. When I have the Open event of the Canvas, I know it is resizing correctly. A “blank” picture is showing correctly.

//DropObj
mImage = obj.Picture
Me.Invalidate

In the Paint event of the Canvas I have this:
g.DrawPicture(mImage, 0,0, g.Width, g.Height, 0,0,mImage.Width, mImage.Height)

And I am getting a NIL exception …

See the “ObjectsInCanvas” example application that comes with Real Studio / Xojo. This helped me get started and it sounds like you are heading down the same road. :slight_smile: Check the code out.

It doesn’t have resize code, but it will help you.

Solved …

mImage=Picture.Open(obj.FolderItem)

i used that function a lot my all my application

mImage=Picture.Open(obj.FolderItem) by itself does not resize picture to a ImageWell, would you mind posting the complete code?
Thanks.
Lennox

Here it is.

Defined mImage in the Properties of the Window as Picture. Also set the FileTypes to handle jpeg in the App.

For the Canvas, defined for the Events:

DropObject:

If Obj.PictureAvailable then
mImage=obj.picture
Elseif Obj.FolderItemAvailable then
mImage=Picture.Open(obj.FolderItem)
End if

Me.Invalidate

Open:

Me.AcceptPictureDrop
me.AcceptFileDrop(“image/jpeg”)

mImage=EmBranco <= this is the “blank”/ standard picture when the Canvas open.

Paint:

// This is the resize
g.DrawPicture(mImage, 0,0, g.Width, g.Height, 0,0,mImage.Width, mImage.Height)

// The below is just a rectangle around the frame
g.ForeColor = &cA1CBFF
g.DrawRect( 0, 0, g.Width, g.Height )

Also please see the “DragPicture” example that comes with XOJO.

[quote=14665:@Sam Rowlands]On Cocoa, you can get the OS to do it for you. Put this code in the Open event of an ImageWell.

// - (void)setImageScaling:(NSImageScaling)scaling declare sub setImageScaling lib "Cocoa" selector "setImageScaling:" ( handle as integer, value as integer ) setImageScaling( me.handle, 0 )[/quote]

with this in Open - ImageWell handles Drop automatically and you can use Cut, Copy, Paste, Delete from the Edit Menu

  declare sub setEditable lib "Cocoa" selector "setEditable:" _
  ( handle as integer, value as Boolean )
  setEditable( me.handle, true )

Example ImageWell

[quote=167520:@Axel Schneider]with this in Open - ImageWell handles Drop automatically and you can use Cut, Copy, Paste, Delete from the Edit Menu

  declare sub setEditable lib "Cocoa" selector "setEditable:" _
  ( handle as integer, value as Boolean )
  setEditable( me.handle, true )

[/quote]
Expanding on that how might one detect a change to the ImageWell when a user Pastes?

[quote=14688:@Eugene Dakin]Hello,

I have an example for a Canvas in a Real Studio project at Click Here. Not sure if this is adaptable to an imagewell for Xojo, and it is worth a try. :slight_smile:

Sincerely,

Eugene[/quote]

Hi Eugene,

Could you please upload that file again.

Thanks.

Lennox

ImageWell1.picture = ReSizeImage(pic,ImageWell1.width,ImageWell1.Height)

[code]fuction ReSizeImage (Pic as Picture,SetWidth As Integer=240,SetHeight As Integer=320) as picture
If pic = Nil Then Return Nil
Dim Width,Height As Integer
Dim imageWidth As Integer = pic.Width
Dim ImageHeight as Integer = pic.Height

if(setWidth/imageWidth<=setHeight/imageHeight) then
width=imageWidth*(setWidth/imageWidth)
height=imageHeight*(setWidth/imageWidth)
else
Width=imageWidth*(setHeight/imageHeight)
Height=imageHeight*(setHeight/imageHeight)
end if

Dim NewSizePic as new Picture(Width,Height,32)
NewSizePic.Graphics.DrawPicture(Pic, 0, 0, Width, Height, 0, 0, pic.Width, pic.Height)
Return NewSizePic
end fuction[/code]

Thanks,
I’ll try it.
Lennox

[quote=414561:@Lennox Jacob]Hi Eugene,

Could you please upload that file again.

Thanks.

Lennox[/quote]

I will post it when i am back on my developer computer in a day or two. :slight_smile: