Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 27-Feb-13 6:53am
Edited 27-Feb-13 6:56am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
afshinsh at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
afshinsh at 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)

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100