Click here to Skip to main content
15,896,318 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I want to create an image file and print it afterward, but unfortunately when I want to print it, the quality is low, the following is part of the source code I have used.

If you have any hint including the changes in source code to solve the problem, I would appreciate your comments.

Regards
private void btnCreateImage_Click(object sender, EventArgs e)
 {
     Font font = new Font("Tahoma", 10F, FontStyle.Bold);
     Brush brush = new SolidBrush(Color.Black);
     StringFormat format = new StringFormat();
     format.Alignment = StringAlignment.Center;
     format.LineAlignment = StringAlignment.Center;

     Pen pen = new Pen(Color.Blue, 1);
     Rectangle rectangle = new Rectangle(100, 100, 200, 200);


     Bitmap image = new Bitmap(600, 850);
     Graphics graphic = Graphics.FromImage(image);

     graphic.DrawRectangle(pen, rectangle);
     graphic.DrawString(txtTest.Text, font, brush, rectangle, format);


     image.Save("EndImage.bmp");

     MessageBox.Show("EndImage.bmp generated successfully in Bin Folder");

     System.Diagnostics.Process.Start("EndImage.bmp");

     image.Dispose();
     graphic.Dispose();
 }
Posted
Updated 27-Feb-13 5:56am
v2

I think Marcus Kramer is on the right track. I experimented with setting the resolution of the new bitmap and got better results. Below is from my test program. Some of the other properties I experimented with are commented out below. You may or may not want to do further experimentation with those properties.
Font font = new Font("Tahoma", 10, FontStyle.Regular);
Brush brush = new SolidBrush(Color.Black) ;
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
Pen pen = new Pen(Color.Blue,1);
Rectangle rectangle = new Rectangle(100, 100, 600, 600);
Bitmap image = new Bitmap(800, 800);
image.SetResolution(300, 300);
Graphics graphic = Graphics.FromImage(image);
//graphic.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphic.DrawRectangle(pen, rectangle);
//graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
//graphic.TextContrast = 0;
graphic.DrawString("Test", font,  brush, rectangle, format);
image.Save("EndImage.bmp");
MessageBox.Show("EndImage.bmp generated successfully in Bin Folder");
System.Diagnostics.Process.Start("EndImage.bmp");
image.Dispose();
graphic.Dispose();


Tested on Visual Studio 2012
 
Share this answer
 
Comments
albert sh 27-Feb-13 16:25pm    
Thanks Mike for your comments and Kindly sending your changed source codes.
I tested it , after using it I have better quality but there is two major problems:
1) after using it the size of the text is bigger than the size that I need to print.
2) the quality is still not the same as when using e.graphics
Do you know how I can use e.graphics and then make an image file from it and later on when deciding to print , print it directly or through e.graphics for having good quality?
THanks in advance
Mike Meinz 27-Feb-13 16:58pm    
When I develop software (VB .NET) that will print a report, I use the PrintDocument, PrintDialog and PrintPreviewDialog components found in the Printing item within the Visual Studio Toolbox. These components can be used in C#, also. Using these components, the resulting document to be printed has high-quality text. There is example source code in the Visual Studio Help file.

There is also Print a Form by Using the PrintForm Component but I have never used it. It appears to be part of the Visual Basic PowerPack.
This is because when you create an image for screen viewing it will be either 72 or 96 dpi.
Printing a high resolution image requires at least 300 dpi.
 
Share this answer
 
Comments
albert sh 27-Feb-13 16:33pm    
Hi Marcus,
I know the problem is because of the differentiation of their resolution, but how I can create an image file with the same quality of using e.graphics for later printing.
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900