Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Need help with screen capture

Discussion in 'General Graphics' started by TheWebExpert, Jul 18, 2023.

  1. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    Code (CSharp):
    1.  
    2.     private IEnumerator CutSpriteFromScreen()
    3.       {
    4.         string folderPath = Application.persistentDataPath; // the path of your project folder
    5.         string screenshotName = "Screenshot_" + System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + ".png";
    6.         yield return new WaitForEndOfFrame();
    7.         int width  = 783, height = 1050, startX = 569, startY = 12;
    8.         var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
    9.         Rect rex = new Rect(startX, startY, width, height);
    10.         tex.ReadPixels(rex, 0, 0);
    11.         tex.Apply();
    12.         var bytes = tex.EncodeToPNG();
    13.         Destroy(tex);
    14.         File.WriteAllBytes(System.IO.Path.Combine(folderPath, screenshotName), bytes);
    15.       }
    16.  
    This is my code currently. It does indeed capture the specified portion of the screen, but at VERY low resolution. The image on the screen is sharp; the captured image is not. I need this to be super-high resolution, for printing professionally. Can anyone help with this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Any reason you're not just using ScreenCapture.CaptureScreenshot?
     
  3. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    Yes, because I wanted to capture only a portion of the screen. Unless there's a way to crop the image after the fact...
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    yes you can use getpixels and setpixels on a new texture
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please use the General Graphics forum for this as none of this API is part of the 2D feature set.

    I'll move your post for you.
     
  6. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    Could you provide me a code sample for the getpixels/setpixels?
     
  7. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    547
    You are saying the PNG does not have a size of 783 x 1050? What's your window size?

    By the way, the selected region is taller than it is wide. Is that intentional?
     
  8. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    Code (CSharp):
    1.    
    2.         private IEnumerator CutSpriteFromScreen()
    3.           {
    4.      
    5.  
    6.             int width  = 783, height = 1050, startX = 569, startY = 12;
    7.  
    8.  
    9.  
    10. Color[] p = screenshotToCrop.GetPixels(startX, startY, width , height);
    11.  
    12.             var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
    13.  
    14. tex.SetPixels(p);
    15.    
    16.  
    17.             tex.Apply();
    18.             var bytes = tex.EncodeToPNG();
    19.             Destroy(tex);
    20.             File.WriteAllBytes(System.IO.Path.Combine(folderPath, screenshotName), bytes);
    21.           }
    22.    
    23.  
     
  9. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    screenshotToCrop is not a valid Unity command.
     
  10. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    Q: You are saying the PNG does not have a size of 783 x 1050?
    A: Yes, it actually does.

    Q: What's your window size?
    A: Currently, 1920x1080.

    Q: By the way, the selected region is taller than it is wide. Is that intentional?
    A: Yes; it's representative of an 8 1/2 x 11 page.

    I'm guessing that to start, I need to increase the resolution of my window. I'm going to try that today.
     
  11. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    Ok, my window size is now 3840 x 2160, and the image size is now 1528 x 2056.
    The resolution is MUCH improved. I'm wondering if I can get it even better...
     
  12. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    My window size is now 7680 x 4320, and the image size is now 3022 x 4056.
    The resolution is stunning. I don't think I'm going to mess with it any more.
     
    c0d3_m0nk3y likes this.
  13. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    547
    You could render to a render target that is bigger than the screen but no need to go through the effort if a big window works too.