How to print a full size image in c# - c#

I am trying to print an image in C#. It is a full 8.5x11 size tiff created by Adobe Acrobat from a PDF. When I print it with C# using the code below, it prints correct vertically, but not horizontally, where it is pushed over about a half inch. I set the origin of the image to 0,0. Am I missing something?
private FileInfo _sourceFile;
public void Print(FileInfo doc, string printer, int tray)
{
_sourceFile = doc;
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printer;
pd.DocumentName = _sourceFile.FullName;
using (Image img = Image.FromFile(_sourceFile.FullName)) {
if (img.Width > img.Height) {
pd.DefaultPageSettings.Landscape = true;
}
}
pd.PrintPage += PrintPage;
foreach (PaperSource ps in pd.PrinterSettings.PaperSources) {
if (ps.RawKind == tray) {
pd.DefaultPageSettings.PaperSource = ps;
}
}
pd.Print();
}
private void PrintPage(object o, PrintPageEventArgs e)
{
using (System.Drawing.Image img = System.Drawing.Image.FromFile(_sourceFile.FullName)) {
Point loc = new Point(0, 0);
e.Graphics.DrawImage(img, loc);
}
}

look at the code from this below for a good example this code is from this link below
Print Image in C#
private void PrintImage()
{
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.OriginAtMargins = false;
pd.DefaultPageSettings.Landscape = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
printPreviewDialog1.Document = pd;
printPreviewDialog1.ShowDialog();
//pd.Print();
}
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
double cmToUnits = 100 / 2.54;
e.Graphics.DrawImage(bmIm, 100, 1000, (float)(15 * cmToUnits), (float)(10 * cmToUnits));
}
private void button5_Click(object sender, EventArgs e)
{
PrintImage();
}

Related

Print formatted Text with Images

I have been searching for long time to print formatted text and images which are in panel(PanelContain) but I have not get yet to print formatted text and images.Please tell me which text box support early I had used Rich text box but it does not convert in bitmap so I was getting blank text box.
Please help me.It is very very important for my project.
This is my code:
private void PrintPanel()
{
System.Drawing.Printing.PrintDocument doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
doc.Print();
}
private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
RichTxtRptBody.BorderStyle = BorderStyle.None;
Bitmap bmp = new Bitmap(PanelContain.Width,
PanelContain.Height);
float tgtWidthMM = 210; //A4 paper size
float tgtHeightMM = 297;
float tgtWidthInches = tgtWidthMM / 25.4f;
float tgtHeightInches = tgtHeightMM / 25.4f;
float srcWidthPx = bmp.Width;
float srcHeightPx = bmp.Height;
float dpiX = srcWidthPx / tgtWidthInches;
float dpiY = srcHeightPx / tgtHeightInches;
bmp.SetResolution(dpiX, dpiY);
PanelContain.DrawToBitmap(bmp, PanelContain.ClientRectangle);
e.Graphics.InterpolationMode =
InterpolationMode.HighQualityBicubic;
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
e.Graphics.DrawImage(bmp, 3, 1, tgtWidthMM, tgtHeightMM-24);
}
catch (Exception ex)
{
}
}
private void toolStripBtnPrint_Click(object sender, EventArgs e)
{
try
{
Img = null;
PrintDocument doc = new PrintDocument();
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
PrintPanel();
}
}
catch (Exception ex)
{
}
}
I have also given a snapshot of my panel:
YOU can use webbrowsercontrol populate dynamically and then you can print easily as you want to print with full formatting option as html code can help you to give layout also.
You want to print a rich text box content with formatting
Try this :
Here eintragRichTextBox is a RichTextBox
private void druckenPictureBox_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
PrintDocument documentToPrint = new PrintDocument();
printDialog.Document = documentToPrint;
if (printDialog.ShowDialog() == DialogResult.OK)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
documentToPrint.Print();
}
}
private void DocumentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
float LinesPerPage = 0;
float YPosition = 0;
int Count = 0;
float LeftMargin = e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
string Line = null;
Font PrintFont = this.eintragRichTextBox.Font;
SolidBrush PrintBrush = new SolidBrush(Color.Black);
LinesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics);
while (Count < LinesPerPage && ((Line = reader.ReadLine()) != null))
{
YPosition = TopMargin + (Count * PrintFont.GetHeight(e.Graphics));
e.Graphics.DrawString(Line, PrintFont, PrintBrush, LeftMargin, YPosition, new StringFormat());
Count++;
}
if (Line != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
PrintBrush.Dispose();
}

Print Preview shows "Document does not contain any pages" instead of the document

I am trying to set up an application in VS2013 with Page Setup, Print Preview and Print. The issue I am having is instead of seeing the page in the Print Preview window, all I see is "Document does not contain any pages." Everything I find on the Internet is regarding opening a file and printing/previewing it. I want to be able to print/preview unsaved text in a Rich Textbox.
Here is what I have:
PageSetupDialog psdlg = new PageSetupDialog();
PrintDialog pdlg = new PrintDialog();
PrintPreviewDialog ppdlg = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
Event handlers:
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
psdlg.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
psdlg.PageSettings = new System.Drawing.Printing.PageSettings();
psdlg.EnableMetric = false;
psdlg.ShowNetwork = true;
pd.PrinterSettings = psdlg.PrinterSettings;
psdlg.ShowDialog();
if (psdlg.PageSettings != null)
{
pd.DefaultPageSettings = psdlg.PageSettings;
}
}
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ppdlg.ShowDialog() == DialogResult.OK)
{
pd.DocumentName = strCurrentFile;
ppdlg.Document = pd;
}
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
if(printDialog1.ShowDialog() == DialogResult.OK)
{
pd.PrintPage += new PrintPageEventHandler (pd_PrintPage);
pd.Print();
}
}
PrintPage Event Handler:
private void DocumentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
StringReader reader = new StringReader(rtbMain.Text);
float LinesPerPage = 0;
float YPosition = 0;
int Count = 0;
float LeftMargin = e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
string Line = null;
Font PrintFont = this.rtbMain.Font;
SolidBrush PrintBrush = new SolidBrush(Color.Black);
LinesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics);
while (Count < LinesPerPage && ((Line = reader.ReadLine()) != null))
{
YPosition = TopMargin + (Count * PrintFont.GetHeight(e.Graphics));
e.Graphics.DrawString(Line, PrintFont, PrintBrush, LeftMargin, YPosition, new StringFormat());
Count++;
}
if (Line != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
PrintBrush.Dispose();
}

Print the entire area of the control with C#

This is not a duplicat question- the diffenece beetween my question an the others one is my Controler contail a scroller, so they are more informations can't be printed.
I have a C# application that contains a main form name MainForms. This MainForms has a control mainDisplay. I want to print the entire information what we found on the mainDisplay to the printer.
The problem is the information on the the control is too big, and I have to scroll to see all information.
Someone have any function that allow me to print this control MainDisplay with entire information in it?
This the printscreen of the area of my MainDisplay at the right you see the scrollbar:
I use this Function (Source : Printing a control)
private static void PrintControl(Control control)
{
var bitmap = new Bitmap(control.Width, control.Height);
control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
var pd = new PrintDocument();
pd.PrintPage += (s, e) => e.Graphics.DrawImage(bitmap, 100, 100);
pd.Print();
}
But my problem still can't print all the informations contain in my control, it's just print a small erea, still need print more informations which are not printed.
I find the solution. This is the steps i do :
1 - We have a mainForm, and this main form contain a control mainDisplay with a specific dimension and area, let's say this dimensions is smaller and we get scroll.
2- What i do is i make this mainDisplay Empty.
3- i create an other Control myControlToDisplay. I draw and i put all fields i want without scroll, so this one myControlToDisplay will have a big dimension.
4- on the star-up of my application, i tell to the mainDisplay to load myControlToDisplay. This time all the content of myControlToDisplay will be display on mainDisplay with a scroll. Because mainDisplay have a small area.
5- I write this functions :
Bitmap MemoryImage;
PrintDocument printDoc = new PrintDocument();
PrintDialog printDialog = new PrintDialog();
PrintPreviewDialog printDialogPreview = new PrintPreviewDialog();
Control panel = null;
public void Print(Control pnl)
{
DateTime saveNow = DateTime.Now;
string datePatt = #"yyyy-M-d_hh-mm-ss tt";
panel = pnl;
GetPrintArea(pnl);
printDialog.AllowSomePages = true;
printDoc.PrintPage += new PrintPageEventHandler(Print_Details);
printDialog.Document = printDoc;
printDialog.Document.DocumentName = "Document Name";
//printDialog.ShowDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}
public void PrintPreview(Control pnl)
{
DateTime saveNow = DateTime.Now;
string datePatt = #"yyyy-M-d_hh-mm-ss tt";
panel = pnl;
GetPrintArea(pnl);
printDoc.PrintPage += new PrintPageEventHandler(Print_Details);
printDialogPreview.Document = printDoc;
printDialogPreview.Document.DocumentName = "Document Name";
//printDialog.ShowDialog();
if (printDialogPreview.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}
private void Print_Details(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
RectangleF marginBounds = e.MarginBounds;
DateTime saveNow = DateTime.Now;
string datePatt = #"M/d/yyyy hh:mm:ss tt";
//String dtString = saveNow.ToString(datePatt);
// create header and footer
string header = "Put all information you need to display on the Header";
string footer = "Print date : " + saveNow.ToString(datePatt);
Font font = new Font("times new roman", 10, System.Drawing.FontStyle.Regular);
Brush brush = new SolidBrush(Color.Black);
// measure them
SizeF headerSize = e.Graphics.MeasureString(header, font);
SizeF footerSize = e.Graphics.MeasureString(footer, font);
// draw header
RectangleF headerBounds = new RectangleF(marginBounds.Left-80, marginBounds.Top-80, marginBounds.Width, headerSize.Height);
e.Graphics.DrawString(header, font, brush, headerBounds);
// draw footer
RectangleF footerBounds = new RectangleF(marginBounds.Left-80, marginBounds.Bottom - footerSize.Height+80, marginBounds.Width, footerSize.Height);
e.Graphics.DrawString(footer, font, brush, footerBounds);
// dispose objects
font.Dispose();
brush.Dispose();
}
public void GetPrintArea(Control pnl)
{
MemoryImage = new Bitmap(pnl.Width, pnl.Height);
Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
}
protected override void OnPaint(PaintEventArgs e)
{
if (MemoryImage != null)
{
e.Graphics.DrawImage(MemoryImage, 0, 0);
base.OnPaint(e);
}
}
void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Rectangle pageArea = e.PageBounds;
Rectangle m = e.MarginBounds;
if ((double)MemoryImage.Width / (double)MemoryImage.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)MemoryImage.Height / (double)MemoryImage.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)MemoryImage.Width / (double)MemoryImage.Height * (double)m.Height);
}
e.Graphics.DrawImage(MemoryImage, m);
}
6 -And finally we suppose that we have two buttons, one to Print and the other one to print preview.
You just have to call this function :
private void PrintButton_Click(object sender, EventArgs e)
{
try
{
Print(mainDisplay.getCurentPanel());
}
catch (Exception exp)
{
MessageBox.Show("Error: \n" + exp.Message);
}
}
private void PrintPreviewButton_Click(object sender, EventArgs e)
{
try
{
PrintPreview(mainDisplay.getCurentPanel());
}
catch (Exception exp)
{
MessageBox.Show("Error: \n" + exp.Message);
}
}
Hope it will help someone :)
good luck

Show Print Dialog before printing

I want to show the print dialog box before printing the document, so the user can choose another printer before printing. The code for printing is:
private void button1_Click(object sender, EventArgs e)
{
try
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintImage);
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ToString());
}
}
void PrintImage(object o, PrintPageEventArgs e)
{
int x = SystemInformation.WorkingArea.X;
int y = SystemInformation.WorkingArea.Y;
int width = this.Width;
int height = this.Height;
Rectangle bounds = new Rectangle(x, y, width, height);
Bitmap img = new Bitmap(width, height);
this.DrawToBitmap(img, bounds);
Point p = new Point(100, 100);
e.Graphics.DrawImage(img, p);
}
will this code be able to print the current form?
You have to use PrintDialog
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintPage);
PrintDialog pdi = new PrintDialog();
pdi.Document = pd;
if (pdi.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
else
{
MessageBox.Show("Print Cancelled");
}
Edited(from Comment)
On 64-bit Windows and with some versions of .NET you may have to set pdi.UseExDialog = true; for the dialog window to appear.
For the sake of completeness, the code should include a using directive
using System.Drawing.Printing;
for further reference please goto
PrintDocument Class

Print with BufferedGraphics using PrintDialog

I have a BufferedGraphics called backbufferGraphics and I want to print out the content in that buffered by using a PrintDialog, this is my code but it's not work:
private print()
{
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
printDialog1.Document = printDocument1;
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
printDialog1.ShowDialog();
}
void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
DrawToPrint(e.Graphics);
}
private void DrawToPrint(Graphics _Gd)
{
backbufferGraphics.Render(_Gd);
}
Thanks!
you have first to set your buffergraphics in a image and after you will print image:
Bitmap bmp = new Bitmap();
int X = e.X - bmp.Width/2;
int Y = e.Y - bmp.Height/2;
// créer et initialiser le BufferedGraphics
BufferedGraphics bg =
BufferedGraphicsManager.Current.Allocate(this.CreateGraphics(),
ClientRectangle);
Graphics g = bg.Graphics;
g.Clear(SystemColors.Control);
g.DrawImage(bmp, new Point(X, Y));
bg.Render();
g.Dispose();
bg.Dispose();
//save bmp as file

Categories

Resources