1

I am the beginner on unity, I want to print an image directly to connected default printer without preview.

I am using this code for print but it takes the preview

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class PrintImage : MonoBehaviour
{

public void PrintFile()
{
    PrintFiles();
}

void PrintFiles(string path=null)
{

    path = "file:///C:/Users/ersai/Desktop/2.jpg";
    System.Diagnostics.Process process = new System.Diagnostics.Process(); 
    process.StartInfo.CreateNoWindow = false; 
    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process.StartInfo.UseShellExecute = true;
    process.StartInfo.FileName = path; 
    process.StartInfo.Verb = "print";

    process.Start(); 

  }
 }

That's was not duplicate because this was not a window tag question I am asking about C# unity the question which tagged duplicate was not working with C# Unity. I am solved by LSPrinter Simple from assest store of unity.

DLeh
  • 23,806
  • 16
  • 84
  • 128
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
  • 1
    The `CreatNoWindow` variable should be set to `true` not `false`... – Programmer Mar 09 '18 at 08:30
  • 3
    Possible duplicate of [Print images c#.net](https://stackoverflow.com/questions/5750659/print-images-c-net) – Chuck Savage Mar 09 '18 at 09:22
  • 1
    @ChuckSavage Not really a duplicate. The working answer from tha question uses `PrintDocument` from the Windows Form API which will make this code only work on Windows and there is no windows tag in this question. – Programmer Mar 09 '18 at 10:49
  • Thankyou Programmer CreatBiWindo to true not working and that's the question not duplicate it's not window tag question it about unity but ChuckSavag thanks for the comment and I solved using LSPrinter from assest store it's useful – Arjun saini Mar 10 '18 at 06:51
  • @Programmer He was using process, and there's another answer on that question that uses process. It's a duplicate imo, but the OP figured out a unity solution, so that is probably better. – Chuck Savage Mar 10 '18 at 10:07
  • @ChuckSavage I saw that answer before commenting. It did not solve OP's problem which is *"print with default printer [without preview.]"*. Although, it shows how to print. – Programmer Mar 10 '18 at 16:00
  • @Arjunsaini No problem. If you have found a solution please post it. It will help other Unity users in the future. – Programmer Mar 10 '18 at 17:00

2 Answers2

2

I solve by using Ls Printer

If printerName is empty or null, it will print to your default printer.

Currently, it works on Windows. Every printer should work.

using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System;
using System.IO;
using LCPrinter;
using UnityEngine.UI;

public class LCExampleScript : MonoBehaviour {

public Texture2D texture2D;
public string printerName = "";
public int copies = 1;

public InputField inputField;

public void printSmileButton()
{

    //print the texture2d using on
    // Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);*
    Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);
}

public void printByPathButton()
{
   //direct path which fill in inputfield
    Print.PrintTextureByPath(inputField.text.Trim(), copies, printerName);
}
}

it takes a small preview

System.Diagnostics.Process.Start("mspaint.exe", "/pt Assets\\Resources\\"+files);
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
1

May be Im late but following code seems to be working for me in Unity 2018

void Start()
{
    string printerName = "Canon TS8100 series";
    string _filePath = "C:\\ImagesFolder" + "\\1.jpg";
    string fullCommand = "rundll32 C:\\WINDOWS\\system32\\shimgvw.dll,ImageView_PrintTo " + "\"" + _filePath + "\"" + " " + "\"" + printerName + "\"";
    PrintImage(fullCommand);
}

void PrintImage(string _cmd)
{
    try
    {
        Process myProcess = new Process();
        //myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.StartInfo.UseShellExecute = false;
        myProcess.StartInfo.FileName = "cmd.exe";
        myProcess.StartInfo.Arguments = "/c " + _cmd;
        myProcess.EnableRaisingEvents = true;
        myProcess.Start();
        myProcess.WaitForExit();
    }
    catch (Exception e)
    {
        UnityEngine.Debug.Log(e);
    }
}
Saad Anees
  • 1,255
  • 1
  • 15
  • 32