Search Unity

Print the Screen Shot by printer from Unity

Discussion in 'Scripting' started by schetty, Apr 25, 2013.

  1. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    Hi there,


    I have struggle with my project now, i need to take the print out from my unity scene.

    Actually i have a scene which is have some model, i have a button in this screen when i click the button the screen should be take screenshot and it should be print in my printer. This is my function.

    For the screen shot we can use
    Code (csharp):
    1. Application.ScreenShot("Image.png")
    But i dont know how to print this image by the printer.
    Please help me to do this.

    Thanks.
     
  2. small child

    small child

    Joined:
    Apr 25, 2013
    Posts:
    1
    This is my first reply, but I can not help you , I think you should be search the printer how to work and is there a interface work in the unity.
     
  3. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
  4. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  7. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    I am using the unity 3.5.7 (no pro) when check below line i can't get.
    Code (csharp):
    1. using System.Drawing.Printing;
    is this drawing function available in unity ?
     
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    System.Drawing is not available in Unity.

    I see two possible solutions:

    1. Use Application.CaptureScreenshot to make and save the screenshots and make a satellite application (a service maybe) that will watch the folder where the images appear and print them automatically.

    2. It is still possible to use unmanaged code and call WinAPI functions in Unity, even in the free version. But it's the hardest way, I guess.
     
  9. MarigoldFleur

    MarigoldFleur

    Joined:
    May 12, 2012
    Posts:
    1,353
    If you don't have Pro, you're not going to really be able to ensure this works. I think a valid question is "why are you trying to do this anyway?" I mean, the last thing I want is a game wasting my ink/toner by printing more than zero screenshots.
     
  10. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    Actually i need to print out the each and every object functions, when user interact with my scene they might have get more information, so they need to print out that information so that i need one button on my scene when the user click the button the corresponding scene(display) should print by printer.
    This is the task assigned by my project manager.

    and another doubt i have i got this line from some where in net "using System.Drawing.Printing"
    i have tried in my monodevelop but it didn't come, may be this will come only unity 4 versions?

    if i will install pro can i get this?
     
  11. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    No.

    Try this:
    Code (csharp):
    1. System.Diagnostics.Process.Start("mspaint.exe", "/pt d:\\screenshots\\sample.jpg");
     
  12. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    so we can't do direct print ?
     
  13. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    i have wrote the below line
    Code (csharp):
    1.  
    2. if(GUI.Button(Rect(10,10,100,30), "print"))
    3. {
    4. System.Diagnostics.Process.Start("mspaint.exe","D:\\Unity\\Unity Projects\\JPEG Encoding\\Assets\\unity"+count+".png");
    5. }
    6.  
    When i press the "E" key the screen shot will save to this path "D:\\Unity\\Unity Projects\\JPEG Encoding\\Assets\\unity"+count+".png"
    count will increase the each time you press the E button like this:
    unity0.png, unity1.png, etc.,
    Now i press the print button the the last image will be open in paint but its always open the first image , i dont know whats the problem in this.
    Please help me to solve this.
     
  14. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    Code (csharp):
    1. // Update is called once per frame
    2.     void Update () {
    3.        
    4.         if(Input.GetKeyDown(KeyCode.E))
    5.         {
    6.              Application.CaptureScreenshot("Screenshot"+count+".png");
    7.             count++;
    8.         }
    9.    
    10.     }
    11.     void OnGUI()
    12.     {
    13.         if(GUI.Button(new Rect(10,10,100,30), "print"))
    14.         {
    15.             System.Diagnostics.Process.Start("mspaint.exe","Screenshot"+count+".png");
    16.         }
    17.     }
     
  15. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    but its not run properly after i took built.
     
  16. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    I have tried to import this line in my scripting, but my monodevelop does not support this.
    Code (csharp):
    1.  
    2. using.System.Drawing;
    3. using.System.Drawing.Printing;
    4.  
    I am using the unity 3.5.7, i dont know what the problem is this.
    can we use this drawing property in unity 3d?

    Thanks.
     
  17. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    You just can't use System.Drawing[.xxx]. It heavily uses Windows GDI API and is not supported in Unity.
     
  18. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Code (csharp):
    1. string filename = @"d:\screenshot.png";
    2.            
    3. if (File.Exists(filename))
    4. {
    5.     File.Delete(filename);
    6. }
    7.  
    8. Application.CaptureScreenshot(filename);
    9. System.Diagnostics.Process.Start("mspaint.exe", "/pt " + filename);
     
  19. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    if i use any DLL and integrate with the system dll then can we call the printer through the scripting?
     
  20. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I didn't get the idea.
     
  21. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    If by that you mean that you want to put all your printing code in a DLL that references System.Drawing.Whatever, build it outside of Unity, and then use that in your Unity project then the answer is still no.

    That DLL will still use the .NET runtime that Unity uses, which still doesn't have any of the System.Drawing classes. This is how dynamic linking works; code from one DLL is not backed into another DLL at link time, but rather it's loaded at runtime. Doesn't matter if you have .NET installed since Unity will only use its own bundled version of .NET.

    You can get around that with building a .NET EXE that does the printing and communicate with it from your Unity code using inter-process communication, but it's far from trivial.

    Actually, alexzzzz's solution is looking rather elegant in its simplicity to me. Why are you not looking into it?
     
  22. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    no,u can't.Because unity does not support that.

    actually what u have to do. r u print an image using printer device.
     
  23. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    I have tried this code to save my image but its not working all the path name is correct i dont know whats the problem in this.
    This is the code:

    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     if(Input.GetKeyDown("e"))
    5.          ScreenshotEncode();
    6. }
    7. function ScreenshotEncode()
    8. {
    9.     count++;   
    10.      filename = Application.dataPath+"unity"+count+".png";
    11.    
    12.     //file will be save the AssetsUnity1.png
    13.     File.WriteAllBytes(filename, encoder.GetBytes());
    14.     Debug.Log(filename);
    15. }
    16.  
    17. //when i press the button the last image file assetsunity1.png should open, but its not happening
    18. function OnGUI()
    19. {  
    20. if(GUI.Button(Rect(10,10,100,30), "print"))
    21. {
    22.     System.Diagnostics.Process.Start("mspaint.exe", Application.dataPath+"unity"+count+".png");
    23. }
    24.  
    25.  
     
  24. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ImageOpen : MonoBehaviour {
    5.    
    6.     int count=0;
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.        
    16.         if(Input.GetKeyDown(KeyCode.E))
    17.         {
    18.             count++;
    19.             Application.CaptureScreenshot(Application.dataPath+"/Screenshot"+count+".png");
    20.            
    21.            
    22.         }
    23.    
    24.     }
    25.     void OnGUI()
    26.     {
    27.         GUI.Label(new Rect(100,100,100,30),count.ToString());
    28.        
    29.         if(GUI.Button(new Rect(10,10,100,30), "print"))
    30.         {
    31.             System.Diagnostics.Process.Start("mspaint.exe",Application.dataPath+"\\Screenshot"+(count)+".png");
    32.         }
    33.     }
    34. }
    35.  
     
  25. el-santia93

    el-santia93

    Joined:
    Jul 11, 2013
    Posts:
    7
    Edit>project settings>player>optimization change .net2.0subset to .net2.0
     
  26. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
  27. ea9006271

    ea9006271

    Joined:
    Nov 6, 2014
    Posts:
    1
    try this...

    if (GUI.Button(new Rect((Screen.width - 200) / 2, (Screen.height - 200) / 2, 200, 200), "Print Image"))
    {
    ProcessStartInfo info = new ProcessStartInfo(path + "/PrintImage.exe");
    //info.Arguments = "F:/Temp/sample-icon.png";//default image width,height
    info.Arguments = "F:/Temp/sample-icon.png,10,10,100,100";//assign Rectangle(x,y,width,height)
    using (Process p = new Process())
    {
    p.StartInfo = info;
    p.Start();
    }
    }
     

    Attached Files:

    Aestial likes this.
  28. iLyxa3D

    iLyxa3D

    Joined:
    Sep 25, 2013
    Posts:
    31
    How to print photo on 10cm*15cm paper, and fit to sheet size, using PrintImage?

    Also, same question to mspaint method?
     
  29. iLyxa3D

    iLyxa3D

    Joined:
    Sep 25, 2013
    Posts:
    31
    Answering own question:

    Code (CSharp):
    1.         string app = Application.streamingAssetsPath + "/PrintImage.exe";      
    2.         string image = Application.streamingAssetsPath + "/image.png,0,0,529,352";   // from 0,0 point of a sheet, resize image to 529x352 points.  calculation: 150mm / 28.346 px/cm = 529 points ,  100mm / 28.346 pm/cm = 352 points
    3.      
    4.         image = "\"" + image + "\"";   // if path contains spaces
    5.         UnityEngine.Debug.Log(image);
    6.  
    7.         ProcessStartInfo info = new ProcessStartInfo(app);
    8.         info.Arguments = image;
    9.         using (Process p = new Process())
    10.         {
    11.             p.StartInfo = info;
    12.             p.Start();
    13.         }
     
    MiguelAtencio likes this.
  30. Sudarmin-Then

    Sudarmin-Then

    Joined:
    Nov 27, 2014
    Posts:
    27
    Hi iLyxa3D,
    would you care to explain how do you get the value of 28.346 px/cm?
    I tried it to print my image using that value to A4 paper, but it is stretched
     
  31. Sudarmin-Then

    Sudarmin-Then

    Joined:
    Nov 27, 2014
    Posts:
    27
    @EA
    Hi ea9006271,
    Where do you get the PrintImage.exe? And is there any other command for example specifying Landscape or Portrait?
     
  32. summitgamesentertainment

    summitgamesentertainment

    Joined:
    Sep 30, 2016
    Posts:
    17
    Just hitting this forum post for printing from Android and IOS devices.

    Goal here is to print a texture or a file saved as .jpg or .png from Unity application.
    The easiest we could figure out is to open the image as a url link in an external browser and let users print it as provided by Android and IOS devices browser feature. But if we could print the image from Unity app directly, connecting to the printer using a wi-fi would be great.

    Any guidance will be appreciated. Thanks in advance. :)