How to send all images of a folder to printer - c#

I need to send all the images in a folder to printer at once. This is possible from windows explorer where we select all the image files, right click and select print to send all the selected images to print dialog from where we can select printer settings and proceed to print. How do I do this from within c# windows form Application?
Edit: I came up with this but it prints only the last page. How should I modify this?
private void printAllCardSheetBtn_Click(object sender, EventArgs e)
{
PrintDocument pdoc = new PrintDocument();
pdoc.DocumentName = "cardsheets";
PrintDialog pd = new PrintDialog();
if(pd.ShowDialog() == DialogResult.OK)
{
PrinterSettings ps = pd.PrinterSettings;
pdoc.PrinterSettings = ps;
pdoc.PrintPage += pdoc_PrintPage;
pdoc.Print();
}
}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
string[] sheetpaths = Directory.GetFiles(_sheetDirectory);
Point point = new Point(0, 0);
foreach (string s in sheetpaths)
{
g.DrawImage(new Bitmap(s), point);
}
}

You can use PrintDocument.
Just get all images from folder, load them to a Bitmap and through a For Loop use PrintDocument to print one by one.
BTW, use PrintPage event and with PrintPageEventArgs you can draw the image in the document to print with Graphics.
Cheers
EDIT: Check this example -> Example

Related

Print a png file however can't seem to print

Good day! I was trying to print a png file using a Picturebox as my button.
However, I can't seem to print. Could you please help me? or Give me a link that will guide me on how to print on a default printer using C# VS 2010
private void pictureBox2_Click(object sender, EventArgs e)
{
using (PrintDocument pd = new PrintDocument())
{
using (PrintDialog printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.Yes)
{
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
}
}
}
private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Image img = Image.FromFile(#"C:\Coke\res10.png");
e.Graphics.DrawImage(img, 0,0);
}
Found a solution for this.
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile("C://tesimage.PNG");
args.Graphics.DrawImage(i, args.PageBounds);
};
pd.Print();
This is a simple one, but easy to miss..
You are doing just about everything right. Just one tiny thing is not 'OK':
You need to change
if (printDialog.ShowDialog() == DialogResult.Yes)
to
if (printDialog.ShowDialog() == DialogResult.OK)
It is not a question, after all, so it shows 'OK' and 'Cancel' not 'Yes' and 'No'.

Reset Windows Default Printer after PrintDialog() and PrintDocument().Print() in C#

Using
using System.Drawing.Printing;
using System.Windows.Forms;
private void Main()
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
PrintDialog pdi = new PrintDialog();
pdi.Document = pd;
// -> preventing PrintDialog from changing the default printer
// -> BUT changing the PrintDocuments-Printer to the selected??
if (pdi.ShowDialog() == DialogResult.OK)
{
pd.Print();
// -> OR reset default Printer?
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
//- do the printing
ev.HasMorePages = false;
}
always changes the default Windows Printer if the user selects a printer with PrintDialog:
How can I prevent the change by PrintDialog? And how to set the selected Printer for PrintDocument
or
Do I need to reset the default printer after printing? How?
or
Am I'm doing something wrong (I need to develop with Net Framework 2.0)
Regards Thomas

Can I print an SSRS Report using PrintDocument?

I have an SSRS report that I need to print from my windows form application using VS 2008 (C#). I want to print this report using PrintDocument. Is there anyway to do this?
Here is what I have:
private void Printing(string pname)
{
PrintDocument printDoc = new PrintDocument();
if (pname.Length > 0)
printDoc.PrinterSettings.PrinterName = pname;
PageSettings ps = new PageSettings();
PaperSize pz = new PaperSize();
pz.Height = 650;
pz.Width = 400;
ps.PaperSize = pz;
printDoc.DefaultPageSettings = ps;
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
printDoc.Print();
}
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
}
I just don't know how to attach the report name to the PrintDocument.
You will need to use the ReportExecution2005.ReportExecutionService and call the Render method with the format set to "IMAGE" and device info data set similar to:
<DeviceInfo>
<OutputFormat>TIFF</OutputFormat>
</DeviceInfo>
The device info can be used for images to control dpi specific parameters used in print controls.

Show a user print preview and execute code if he printed

I have a bitmap I want the user to see before he prints it. So I open for him print preview, if the user decides to print I want to execute some code.
The problem is, printPreviewDialog will not return an answer. This may be because it has only a print button and close button, but no print-and-close so I can know the user decided to print.
If you have a solution for that I'll be happy, if you think it's not the best way to do so please tell me.
code:
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(Print_Page);
PrintPreviewDialog pritdlg = new PrintPreviewDialog();
pritdlg.Document = pd;
if (pritdlg.ShowDialog() == DialogResult.OK)
pd.Print();
else
MessageBox.Show("you have canceled print");
private void Print_Page(object o, PrintPageEventArgs e)
{
e.Graphics.DrawImage(target, 0,0);
}
Subscribe to the EndPrint event of the document you are sending to the printPreviewDialog control, then check the PrintAction in its PrintEventArgs argument.
Example:
private void buttonPrintPreview_Click(object sender, EventArgs e)
{
PrintPreviewDialog printDialog = new PrintPreviewDialog();
printDialog.Document = yourDocument;
yourDocument.EndPrint += doc_EndPrint; // Subscribe to EndPrint event of your document here.
printDialog.ShowDialog();
}
void doc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
if (e.PrintAction == System.Drawing.Printing.PrintAction.PrintToPrinter)
{
// Printing to the printer!
}
else if (e.PrintAction == System.Drawing.Printing.PrintAction.PrintToPreview)
{
// Printing to the preview dialog!
}
}

Printersettings in C#

I am creating a PDF File in my application, and then I print it (works fine.)
When I print this pdf on another computer/Printer it doesn't look the same! I want it so that it always looks the same, on whichever printer I print on.
Maybe I have to set the borders? Like this:
PrinterSettings ps = new PrinterSettings();
ps.DefaultPageSettings.HardMarginX = 0;
ps.DefaultPageSettings.HardMarginY = 0;
But HardMargin is not writable. Have you guys got some ideas?
Try to set up this way:
PrintDocument printDocument1 = new PrintDocument();
var printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = "Printer name";// optional
//printerSettings.PrinterName = "HP Officejet J6400 series";
printDocument1.PrinterSettings = printerSettings;
printDocument1.PrintPage += printDocument1_PrintPage;
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = printDocument1;
// in the dialog, you can set up the paper size, etc.
printDialog1.UseEXDialog = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
Handler function:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//This print form a rich textbox, but you can render pdf here.
//e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
//e.Graphics.PageUnit = GraphicsUnit.Inch;
}

Categories

Resources