Reputation: 670
I am new to writing code, but am learning C# and am making a little bill-making program for my shop. I need to print the form, which is my bill. From searching on the internet, I found this piece of code:
printForm1.Print(this, PrintForm.PrintOption.ClientAreaOnly);
My billing form has two images and one gridviewbox
. This code can print the bill, but quality of the .xps file is poor - even the text is not printing sharp.
How can I increase the print quality of the form?
Upvotes: 1
Views: 3364
Reputation: 9
Following is a basic sample of rendering a large image to the printer:
Bitmap bitmapToPrint;
public void printImage()
{
bitmapToPrint = new Bitmap(3400,4400);
Font font = new Font(FontFamily.GenericSansSerif, 120, FontStyle.Regular);
string alphabet = "abcdefghijklmnopqrstuvwxyz";
Graphics graphics = Graphics.FromImage(bitmapToPrint);
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
graphics.DrawString(alphabet, font, System.Drawing.Brushes.Black, 0, 0);
graphics.DrawString(alphabet, font, System.Drawing.Brushes.Black, 0, 1000);
graphics.DrawString(alphabet, font, System.Drawing.Brushes.Black, 0, 2000);
graphics.DrawString(alphabet, font, System.Drawing.Brushes.Black, 0, 3000);
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
pd.PrinterSettings.PrintToFile = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmapToPrint, new RectangleF(0.0f, 0.0f, 850.0f, 1100.0f));
}
Upvotes: 0
Reputation: 941635
Yes, that doesn't look good unless you have really long arms. The issue is that printers have a much higher pixel resolution than monitors. A decent printer has a 600 dpi (dots per inch) resolution. Monitors by default are 96 dpi although that's finally improving after being stuck on that for decades.
So to print a form the way you do, you have two unpleasant choices. You can print the form so that one pixel on the screen is one pixel on paper. That gives you a really sharp image of the original form but it is about the size of a postage stamp. Or you print the form as big on paper as it is on the screen, what you see happening now. That turns a single pixel on the monitor into a 6 x 6 blob on paper. The result looks very grainy, particularly text looks poorly.
A solution would be to draw the form 6 times larger on the screen and print that. That however doesn't work, you can't make the form bigger than the screen. The only real solution is to draw 6 times larger to the printer. That requires the PrintDocument class. And a bunch of code in its PrintPage event handler to do the drawing. You can't coax the controls to do it for you so that's a bunch of work.
Or use a report generator. They exist to solve this problem. Google ".net report generator" to start shopping.
Upvotes: 4