Search Unity

Image cropping

Discussion in 'General Graphics' started by jeffmooney, Aug 27, 2012.

Thread Status:
Not open for further replies.
  1. jeffmooney

    jeffmooney

    Joined:
    Aug 27, 2012
    Posts:
    1
    Hi,
    I am developing an app, but I need some guidance on how to crop an image that was taken from the camera or gallery. I want a drag able box to select part of the image and then place an image/object on top of the image and save it. Any help is much appreciated.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    Both the Texture2D and WebCamTexture classes have functions called GetPixels that can extract a rectangular area of pixels from the texture. Once you have the pixel data, you can create a new texture with the same dimensions as the extracted area and use the SetPixels function to place the pixel data in the new texture. This gives the same overall effect as cropping, although the original texture isn't modified.
     
  3. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Is there any way to have the user edit or crop their picture? Possibly using a custom shape selector tool (circle or oval)... or anything like that?
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
  5. vanshika1012

    vanshika1012

    Joined:
    Sep 18, 2014
    Posts:
    48
    Hi, Do you have solution of this one. I am struck with same problem.
     
  6. gamedevsw

    gamedevsw

    Joined:
    Jul 21, 2014
    Posts:
    1
    Hi,Did anyone find the solution to this problem.While taking the picture i want to provide image cropping functionality for the user.Below is my code
    void Start ()
    {
    WebCamDevice[] devices = WebCamTexture.devices;
    deviceName = devices[0].name;
    wct = new WebCamTexture(deviceName, Screen.height, Screen.width, 12);
    Debug.Log ("Script has been started");
    plane = GameObject.FindWithTag ("Player");
    plane.renderer.material.mainTexture = wct;
    // plane.renderer.material.shader = shader;
    wct.Play();
    //Texture2D Crop= new Texture2D (plane.renderer.material.mainTexture.width,plane.renderer.material.mainTexture.height);
    }
    public Texture2D heightmap;
    public Vector3 size = new Vector3(100, 10, 100);

    void OnGUI() {
    if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
    TakeSnapshot();
    }

    // For saving to the _savepath
    private string _SavePath = "C:/Images/"; //Change the path here!
    int _CaptureCounter = 0;
    void TakeSnapshot()
    {
    Texture2D screen = Application.CaptureScreenshot("Screenshot123.png");
    Application.CaptureScreenshot("Screenshot123.png");

    Texture2D snap = new Texture2D(wct.width,wct.height);
    //renderer.material.mainTexture = snap;
    //snap.SetPixels(wct.GetPixels());
    snap.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0);
    snap.Apply();
    //System.IO.File.WriteAllBytes(_SavePath + _CaptureCounter.ToString() + ".png",snap.EncodeToPNG());
    // ++_CaptureCounter;
    }
    }
    when user takes the picture an oval mask appears to crop the picture and save to the drive,my problem is the background has to be transparent when i take a screenshot.Can anyone help plz
     
  7. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    thread over a year old and you necro it
     
  8. OxOGau

    OxOGau

    Joined:
    Nov 3, 2014
    Posts:
    4
    Did someone find a solution ?
     
  9. saru15

    saru15

    Joined:
    Jan 20, 2015
    Posts:
    9
    Any solution of how to crop the image into the custom shape geometry?
     
  10. grobm

    grobm

    Joined:
    Aug 15, 2005
    Posts:
    217
    I am looking for the same solution... Andeeeee was on the right track. I think I will continue heading down that road.
     
Thread Status:
Not open for further replies.