Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Capture part of screen

in Questions Posts: 505

Hi All,

Quickie, is it possible to capture and save the whole or part of the screen - like:


MySprite = screen:get(200,200,264,264)

That would enable you to see the image you have created before saving. If not I think it could be a useful feature.

Comments

  • dave1707dave1707 Mod
    edited June 19 Posts: 5,896

    Yes. See image in the documentation. There's a screen copy that lets you copy an area of the screen. You also need to use setContext.

  • Posts: 505

    Hi @dave1707,

    Hmmmmm, not sure that's true - may have missed it but didn't see the copying the screen in the image reference text, only images. To date I have created images in memory and manipulated them and used image:get and image:set to read and write them to other image data which I then use as a sprite. The CAMERA is mentioned as an object but I'm not sure you can use it in this way.

    Can you give me an example of the syntax used for reading the screen?

  • dave1707dave1707 Mod
    Posts: 5,896

    @Bri_G Using setContext, you draw on the screen and then you can copy all or part of the screen. If you can tell me exactly what you're trying to do, maybe I can whip up a good example.

  • dave1707dave1707 Mod
    edited June 19 Posts: 5,896

    @Bri_G Here's an example of drawing something on the screen and then copying a section of it. Move the box in the lower left area and see the copied area in the upper right area.

    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    
    function setup()
        tx,ty=0,0
        scrn=image(400,400)
        setContext(scrn)
        background(154, 206, 223, 255)
        fill(255)
        ellipse(100,200,50)
        ellipse(250,350,60)
        rect(200,100,50,50)
        text("qwertyuiopzxcvbnmqwerty",250,200)
        text("asdfghjklsedcvtyhnkugfs",50,100)
        setContext()
        img=scrn:copy(0,0,200,200)
    end
    
    function draw()
        collectgarbage()
        background(40, 40, 50)
        sprite(scrn,200,200)
        fill(255,255,255,150)
        stroke(255)
        strokeWidth(2)
        rect(tx,ty,200,200)
        sprite(img,700,400)
    end
    
    function touched(t)
        if t.state==MOVING then
            tx=t.x//1
            ty=t.y//1
            if tx>0 and ty~=0 and tx<200 and ty<200 then
                img=scrn:copy(tx,ty,200,200)
            end  
        end
    end
    
  • Posts: 505

    Hi @dave1707,

    Sorry - still don't think this is copying from the screen. You defined an image scrn then wrote to that image in memory under setContext() then copied part of that image as img using scrn:copy() - all in setup. Then in the draw function you just printed those images as sprites to the screen.

    In the touched section you are just repeatedly copying out the same section of scrn which is held in memory.

    That's just how I am operating at the moment - my problem is - want to fill the screen with tiles then copy out a section once I've decided it looks OK. I could do it using a screen sized image and pasting a sprite to the full screen but that uses all the extra memory when you could already have an image on the screen from which you could copy.

    Course I could do it by dumping the screen image to file and externally cutting out the section that I want, but that's not very elegant. A programmable screen copy full or part could be useful.

  • dave1707dave1707 Mod
    Posts: 5,896

    You can create a scrn area the full size of the screen, fill that area with your tiles, then copy out the section you want. I did everything in setup just to keep it simple. Instead of drawing to the screen, just draw everything to the scrn area. After you see what you want, you can then use your finger (or another way) to select the area you want to copy. Maybe I'm just not understanding what you're trying to do.

  • Posts: 207

    @Bri_G What i did when I made my apps is I would make one image. In the draw() function, I would setContext of all the drawing components into that one image, and rather than drawing the objects onto the screen, I drew it into this one image and I would draw the image onto the screen. This was really useful because it allowed me to make the apps universal (looked a bit distorted, but not too big a deal). If you do it like that, then you can just crop out any part you want at anytime!

  • dave1707dave1707 Mod
    Posts: 5,896

    @Bri_G OK, I'm going to try again. Here's another example of what I think you're after. Since I don't know how you're going to create your tiles, I'm just drawing squares on the screen where ever you touch the screen. After you draw what you want, use the x,y,w,h parameters to encompass the area you want to copy. When done, slide the copy parameter and that section of the screen will be copied to the Dropbox folder with the name img. I'm only using one name, but another parameter can be used to enter a name so you can copy other areas. Is something like this closer to what you're after.

    function setup()
        parameter.boolean("copy",false)
        parameter.integer("x",0,WIDTH,5)
        parameter.integer("y",0,HEIGHT,5)
        parameter.integer("w",0,WIDTH,5)
        parameter.integer("h",0,HEIGHT,5)
        scrn=image(WIDTH,HEIGHT)
        setContext(scrn)
        background(208, 229, 228, 255)
        setContext()
    end
    
    function draw()
        background(40, 40, 50)
        setContext(scrn)
        if dr then
            dr=false
            fill(226, 115, 115, 255)
            rect(tx,ty,20,20)
        end    
        setContext()
        sprite(scrn,WIDTH/2,HEIGHT/2)
        noFill()
        stroke(255, 0, 0, 255)
        strokeWidth(2)
        rect(x,y,w,h)
        if copy then
            copy=false
            img=scrn:copy(x,y,w,h)
            saveImage("Dropbox:img",img)
            print("section copied")
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            tx=t.x
            ty=t.y
            dr=true
        end
    end
    
  • edited June 21 Posts: 505

    Hi @dave1707, wow - you work fast - what you produced was just what I wanted to achieve. It is using a buffered image but you are modifying it on the fly and then capture it to Dropbox. All you need now is a counter stored in project variables which can be read when run, incremented and appended to the file name to avoid name clashes; allowing you to play around without exiting.

    Thanks saved me loads a time!!

    @CamelCoder - thanks for your feedback - on the same lines as dave1707.

Sign In or Register to comment.