C# ProcessStartInfo Print Copies To Printer - c#

I want to be able to print multiple copies set from the printer dialogue and send them to the printer.
using (System.Windows.Forms.PrintDialog printerDialog = new System.Windows.Forms.PrintDialog())
{
string printer;
if (printerDialog.ShowDialog() == DialogResult.OK)
{
printer = printerDialog.PrinterSettings.PrinterName;
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(GetDocPath("StickerBlank.docx", Client.ClientID.ToString()));
for (int i = 0; i < printerDialog.PrinterSettings.Copies; i++)
{
Thread.Sleep(3000);
using (var regWord = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe"))
{
//string arguments = String.Format(#"-t ""{0}"" ""{1}""", printerDialo, printer);
if (regWord == null)
{
info.Arguments = "\"" + printer + "\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "Print";
}
else
{
info.Arguments = "\"" + printer + "\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "PrintTo";
}
}
System.Diagnostics.Process.Start(info);
}
}
}
It works but the problem is that the word document which is my template opens as much as copies are set from the printdialog. Is there a way to specify that i want 5 copies as arguments?

Related

c# generate thumbnails from raw images

I have an Asp.net Web forms application,when upload raw image file with format
{ "cr2", "raw", "dng", "nef", "raf", "orf", "srf", "sr2", "arw", "k25", "kdc", "dcr","mos",
"pnx", "crw", "mrw", "pef" , "mef" , "rw2","a7","a7r"}
How can i generate thumbnails from raw image?
You can use dcraw.exe application run on your .net application.you can download it in the link.
First save raw image in local disk and use the code:
string dcrawPath = "dcraw.exe";
ProcessStartInfo startInfo = new ProcessStartInfo();
string inputImagePath= "input Raw Image Path/";
string outputImagePath = "output Raw Image Path/";
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = dcrawPath;
string commandArg1 = string.Format("\"{0}\"", outputImagePath);
string commandArg2 = string.Format("\"{0}\"", inputImagePath);
startInfo.Arguments = "-u ";
startInfo.Arguments += commandArg1;
startInfo.Arguments += " -e ";
startInfo.Arguments += commandArg2;
startInfo.Arguments += " -T";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
string stdout = exeProcess.StandardOutput.ReadToEnd();
string stderr = exeProcess.StandardError.ReadToEnd();
Console.WriteLine("Exit code : {0}", exeProcess.ExitCode);
}
Put raw image in inputImagePath variable
You can find image in outputImagePath variable
Try GDPicture.NET component. It supports different RAW formats and easy for use, but is not free. To generate thumbnail use CreateThumbnail or CreateThumbnailHQ method of GdPictureImaging class:
using (var imaging = new GdPictureImaging())
{
int pictureId = imaging.CreateGdPictureImageFromFile(filepath);
if (pictureId == 0)
{
MessageBox.Show("Error: " + imaging.GetStat().ToString());
return;
}
int thumbnailImgId = imaging.CreateThumbnail(pictureId, 20, 20);
imaging.SaveAsPNG(thumbnailImgId, "thumbnail.png");
imaging.ReleaseGdPictureImage(thumbnailImgId);
imaging.ReleaseGdPictureImage(pictureId);
}

ffmpeg ask for intsall

I am trying to create an image/thumbnail from the video stored in local folder.
Here is what I am doing is-
Process p;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Server.MapPath("~/FFMPEG/ffmpeg.exe");
info.RedirectStandardOutput = false;
info.CreateNoWindow = false;
info.Arguments = " -i " + videopath + " -vframes 1 " + imagepath + "%d.jpg";
info.UseShellExecute = false;
p = Process.Start(info);
while (!p.HasExited) { Thread.Sleep(10); }
When I execute above code a popup box comes to install ffmpeg.exe
If I install the software, for the next time it asks again.
Am I doing some mistake?
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Below is the code
Process p;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Server.MapPath("~/FFMPEG/ffmpeg.exe");
info.RedirectStandardOutput = false;
// below lines will not open the command window
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.Arguments = " -i " + videopath + " -vframes 1 " + imagepath + "%d.jpg";
info.UseShellExecute = false;
p = Process.Start(info);
while (!p.HasExited) { Thread.Sleep(10); }

Cannot access the file because it is being used by another process using ffmpeg

I am getting the error:
Cannot access the file because it is being used by another process
I have a C# desktop app.
I am using the Process class to convert images to a video file by using FFMPEG.
This is my code:
using (Process serverBuild = new Process())
{
serverBuild.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
string args = " -f image2 -i " + {path} + "\\img%05d.jpg -s 352x288 -filter:v \"setpts=5.0*PTS\" -y " + {path}\\File.mp4;
serverBuild.StartInfo.Arguments = args;
serverBuild.StartInfo.FileName = "ffmpeg.exe";
serverBuild.StartInfo.UseShellExecute = false;
serverBuild.StartInfo.RedirectStandardOutput = true;
serverBuild.StartInfo.RedirectStandardError = true;
serverBuild.StartInfo.CreateNoWindow = true;
serverBuild.Start();
// string output = serverBuild.StandardOutput.ReadToEnd();
//Log.Instance.Debug(serverBuild.StandardError.ReadToEnd());
serverBuild.WaitForExit();
serverBuild.Close();
}
Directory.Delete(ExportRoute + FFMPEGPacket.LicenseKey + "\\" + FFMPEGPacket.Guid, true);
//which raise the error..
The images are all deleted but the File.Mp4 is not and that is the error. The error says that the newly created MP4 file cannot be deleted.
NB
This is partial code to illustrate the error
You may try the following code to create the file (it worked for me):
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = exe_path;
// replace your arguments here
psi.Arguments = string.Format(#" arguments ")
psi.CreateNoWindow = true;
psi.ErrorDialog = true;
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = false;
psi.RedirectStandardError = true;
Process exeProcess = Process.Start(psi);
exeProcess.PriorityClass = ProcessPriorityClass.High;
string outString = string.Empty;
exeProcess.OutputDataReceived += (s, e) =>
{
outString += e.Data;
};
exeProcess.BeginOutputReadLine();
string errString = exeProcess.StandardError.ReadToEnd();
Trace.WriteLine(outString);
Trace.TraceError(errString);
exeProcess.WaitForExit();
exeProcess.Close();
exeProcess.Dispose();
FFMPEG might still be rendering the creation of video from images after it closes, so it might be worth if you place a Threading.Thead.Sleep(5000) 5 secs; before delete.
Try that:
File.WriteAllBytes(path, new byte[0]);
File.Delete(path);

execute multi-line cmd command in c#

I am creating java compiler in c# .it is working perfect if there is no input.but it ask forstrong text input it give me error (Exception in thread "main" java.util.NoSuchElementException: No line found)
public void executeit()
{
adresss = "";
string dir = textBox1.Text;
adresss = dir;
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.CreateNoWindow = true;
string[] s = new string[30];
s = Directory.GetDirectories(#"C:\Program Files\Java\", "jdk1*");
info.WorkingDirectory = s[0] + #"\bin\";
info.Arguments = "/c javac " + "\"" + dir + "\"";
Process p = new Process();
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
label1.Text = "";
p.StartInfo = info;
p.Start();
StreamReader sw = p.StandardError;
string err = sw.ReadToEnd();
label1.Text = err;
if (label1.Text == "")
{
label1.Text = "Compiled without Errors";
}
}
and it is java file:
import java.util.Scanner;
public class inputtry {
public static void main (String args[]){
Scanner reader = new Scanner(System.in);
System.out.println("Enter the first number");
//get user input for a
String a=reader.nextLine();
System.out.print("number is:");
System.out.print(a);
}
}

How do I run Command line commands from code

I need to do 2 things: run a batch file (works fine), and run a command (does not work).
The method for the command throws the exception 'file not found'. If I open a cmd window, and type the command, it works perfectly.
private static void Rescan()
{
//System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("DEVCON ReScan");
//psi.RedirectStandardOutput = true;
//psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//psi.UseShellExecute = false;
//System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "DEVCON ReScan";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
System.IO.StreamReader myOutput = proc.StandardOutput;
proc.WaitForExit(4000);
if (proc.HasExited)
{
string output = myOutput.ReadToEnd();
FileIO.WriteLog(_writePath, output);
}
}
The commented code also throws the same exception.
DEVCON ReScan is really the name of the executable? I guess the executable is DEVCON, while ReScan is a parameter. This means you have to set StartInfo.FileName to "DEVCON" and StartInfo.Arguments to "ReScan".
Is the DEVCON application actually in the working directory ?
Otherwise it won't work unless you specify the full path to it.
Furthermore you have to specify the extension, so I suppose you'd go for "Devcon.exe",
and specify the parameters not in the filename but in the parameters :)
Try this:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Environment.GetEnvironmentVariable("comspec");
psi.CreateNoWindow = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
ConsoleColor fc = Console.ForegroundColor;
StreamWriter sw = p.StandardInput;
StreamReader sr = p.StandardOutput;
char[] buffer = new char[1024];
int l = 0;
sw.Write("DEVCON ReScan");
sw.Write(sw.NewLine);
Console.Write(">> ");
l = sr.Read(buffer, 0, buffer.Length);
for (int n = 0; n < l; n++)
Console.Write(buffer[n] + " ");
p.Close();

Categories

Resources