Crop Picture in Variable

I have an image stored in a variable. I want to copy only a portion of the image and put it into a second variable.

I am using the following Variables:

tmpBG As Picture tmpCardListBG As Picture

I need to copy a portion of tmpBG into tmpCardListBG. tmpBG has a pixel size of 1024x768 and tmpCardList has a pixel size of 710x500. tmpCardList needs to copy the contents of tmpBG from Left = 282 and Top = 118.

[quote=91098:@Charles Fasano]I have an image stored in a variable. I want to copy only a portion of the image and put it into a second variable.

I am using the following Variables:

tmpBG As Picture tmpCardListBG As Picture

I need to copy a portion of tmpBG into tmpCardListBG. tmpBG has a pixel size of 1024x768 and tmpCardList has a pixel size of 710x500. tmpCardList needs to copy the contents of tmpBG from Left = 282 and Top = 118.[/quote]

See http://documentation.xojo.com/index.php/Graphics.DrawPicture

You can set the portion in [, SourceX as Integer ] [, SourceY as Integer ]

I already looked there but I was confused as to what to put where and I couldn’t figure out how to put that into another variable.

You need to create new picture and copy portion you need with DrawPicture.

this sample code does a scale:
http://www.monkeybreadsoftware.net/faq-howtoscaleapictureproportinally.shtml

if you pass different parameters to draw picture, you can copy only portion.

You start by creating the picture variable to copy to :

dim p as picture

Then here is what is done. Each element is a part of what you do to copy a portion of an image :

p.Graphics.DrawPicture ( Image as Picture, X as Integer, Y as Integer [,DestWidth as Integer ] [, DestHeight as Integer ] [, SourceX as Integer ] [, SourceY as Integer ] [, SourceWidth as Integer ] [, SourceHeight as Integer] )

‘Image’ is your image. Place the name of your variable that contains the image,
X is usually 0 - X and Y are here the corner of the new picture in your new variable
Y is usually 0 - X and Y are here the corner of the new picture in your new variable
DestWidth is what will be the width of the copy - usually same as what you copy
DestHeight is what will be the height of the copy - usually same as what you copy
Source X is the left of the portion you want to copy. For instance 100 if you want to start copying at 100 points from the left edge
Source Y is the top of the portion you want to copy
SourceWidth is the width of the portion to copy
SourceHeight is the height of the portion to copy.

You will notice that left, top, width and height work exactly the same as a control on a page is positioned and sized.

Now you take a deep breath, and calmly start assembling your call to DrawPicture. The total code needed to do what you want fits in two lines :

dim p as picture p.graphics.DrawPicture( etc.)

After that p contains the portion of image you wanted to copy.

You will succeed. It is important that you do it by yourself to understand fully how it works. It is not that difficult, as you will see. When you think you have assembled correctly all the elements, post what you have done and we can help further.

Good luck :slight_smile:

I already tried

dim p as picture p.graphics.DrawPicture(tmpBG,0,282,118,0,0,710,500)
but I get a NilObjectException even though tmpBG is not Nil and its contents shows the correct image.

tmpBG may not be nil but p certainly is. You need to create a new picture of the required size and depth first.

It’s been awhile since I’ve done this so I forgot to code the dimensions. I used

tmpCardList = New Picture

and received an error that didn’t make any sense to me

Now that I added the dimensions 710,500 I got some of the image but I was using the wrong coordinates. I have finally got it they I want it thanks for the help.

I haven’t touched this code since 2007 so I forgot a lot. Even looking at my old source code I was confused.

[quote=91330:@Charles Fasano]Now that I added the dimensions 710,500 I got some of the image but I was using the wrong coordinates. I have finally got it they I want it thanks for the help.

[/quote]

Congratulations, you got it working. Sorry, I forgot to dim p as new picture and set the dimensions.

An easy way to know what are the coordinates of the portion you want to copy :

  • Place a canvas on a page against the top left corner (0,0)
  • Make the image the backdrop (drag the image into the project and set it as backdrop in the inspector)
  • Drag a new canvas over that and change its dimensions and position to delimit the area you want to copy

Now the inspector will tell you the values you need :

SourceX = Left
SourceY = Top
SourceWidth = Width
SourceHeight = Height