Selective area load of Images

Post Reply
Erg2
Posts: 18
Joined: Sat Dec 10, 2016 8:58 pm
My devices: ipad2
Flag: Australia

Selective area load of Images

Post by Erg2 »

Hi

I’m loading a png image, and setting it to a location on the screen, then using sprite with black drawn rectangles to cover up what I don’t want to display. Not a good way to do it, when you have multiple images and each time you want to display different areas of the other png images.

Is there another way to load only a certain area of the png image?

eg . an area of cordinates from x1, y1 to x2 to y2 of the original png image and displaying this area only to the graphics screen

Thanks

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Selective area load of Images

Post by matt7 »

What about starting a new sprite with the desired final size and then drawing the image to the sprite using negative coordinates to essentially crop the image. Then you can move the sprite where you want and show it or stamp it to the main graphics window.

Here is how I can get a 30x30 portion put of a larger image, at the location 200,200 in the image, and display it at 100,100 on my screen:

Code: Select all

img$ = "Examples/Graphics/images/firtree.png"
spr$ = "Cropped Tree"

GRAPHICS
SPRITE spr$ BEGIN 30,30
  DRAW IMAGE img$ AT -200,-200
SPRITE END
SPRITE spr$ AT 100,100
SPRITE spr$ SHOW

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Selective area load of Images

Post by matt7 »

As a function that extracts an area of an image from x1,y1 to x2,y2 and puts it at destx,desty on the screen:

Code: Select all

img$ = "Examples/Graphics/images/firtree.png"

GRAPHICS
CROP_IMG(img$, 200,200, 230,230, 100,100)

DEF CROP_IMG (name$, x1,y1, x2,y2, destx,desty)

  spr$ = UNIQUE_STR$()

  SPRITE spr$ BEGIN ABS(x2-x1)+1, ABS(y2-y1)+1
    DRAW IMAGE name$ AT -MIN(x1,x2), -MIN(y1,y2)
  SPRITE END

  SPRITE spr$ AT destx, desty
  SPRITE spr$ STAMP
  SPRITE spr$ DELETE

END DEF

Erg2
Posts: 18
Joined: Sat Dec 10, 2016 8:58 pm
My devices: ipad2
Flag: Australia

Re: Selective area load of Images

Post by Erg2 »

Fantastic, Thank you once again Matt

Post Reply