Print a png file however can't seem to print - c#

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'.

Related

print document cannot print specific paper from multiple pages c#

I am facing a problem to print using print document C#. When I am going to print and input 1 to 3 pages from 9 pages using printer setting and then command for print, this command cannot print 1 to 3 pages, this command print by default all documents. How can I fix this?
This is the code:
private void PrintPreview_Click(object sender, EventArgs e)
{
ToolStripButton b = new ToolStripButton();
b.Text = "print";
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);
((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Click += new System.EventHandler(this.button2_Click);
printPreviewDialog1.WindowState = FormWindowState.Maximized;
if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
{
printPreviewDialog1.Show();
}
}
private void Print_Click(object sender, EventArgs e)
{
PrintDialog p1 = new PrintDialog();
p1.AllowSelection = true;
p1.AllowSomePages = true;
if (p1.ShowDialog ()== DialogResult.OK)
{
printDocument1.Print();
}
}

Implement print dialog in my code

I've been trying to solve this myself, but I haven't figured it out yet. I want to open a print dialog window when I'm pressing the btn_print button. I've called out one line I assume is not needed anymore as this is defining the size of the printed page.
Could anyone look at my code and tell me what I could do?
private void btn_print_Click(object sender, RoutedEventArgs e)
{
try
{
PrintDocument pd = new PrintDocument();
//pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while printing", ex.ToString());
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
}
Try something like this:
PrintDocument pd = new PrintDocument();
//pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
System.Windows.Forms.PrintDialog p = new System.Windows.Forms.PrintDialog();
p.Document = pd;
if (p.ShowDialog() == System.Windows.Forms.DialogResult.OK)
pd.Print();

How to send all images of a folder to printer

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

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!
}
}

How can I print a user selected document?

I want to select a file using a file dialog and then print the selected file using the PrintDocument.Print method.
Below is some code which is a partial implementation of what I am trying to accomplish:
using System;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
namespace InstalledAndDefaultPrinters
{
class Program
{
static void Main(string[] args)
{
string filename="";
foreach (string printer in PrinterSettings.InstalledPrinters)
Console.WriteLine(printer);
var printerSettings = new PrinterSettings();
Console.WriteLine(string.Format("The default printer is: {0}", printerSettings.PrinterName));
Console.WriteLine(printerSettings.PrintFileName);
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Open File Dialog";
fdlg.InitialDirectory = #"C:\ ";
fdlg.RestoreDirectory = true;
fdlg.ShowDialog();
Console.WriteLine(fdlg.Title);
if (fdlg.ShowDialog() == DialogResult.OK)
{
filename = String.Copy(fdlg.FileName);
}
Console.WriteLine(filename);
PrintDialog printdg = new PrintDialog();
PrintDocument pd_doc = new PrintDocument();
printdg.ShowDialog();
if (printdg.ShowDialog() == DialogResult.OK)
{
It is at this point that I would like to print the selected file.
pd_doc.Print();
}
}
The code I have above clearly does not do what I need. What alternative approach might steer me in the right direction?
You can solve that using following code snippet.It works as you want.
private void button1_Click(object sender, EventArgs e)
{
PrintDialog printdg = new PrintDialog();
if (printdg.ShowDialog() == DialogResult.OK)
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings = printdg.PrinterSettings;
pd.PrintPage += PrintPage;
pd.Print();
pd.Dispose();
}
}
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(#"C:\Users\therath\Desktop\372\a.jpg");
// You can replace your logic # here to load the image or whatever you want
Point loc = new Point(100, 100);
e.Graphics.DrawImage(img, loc);
}

Categories

Resources