Mykel Stone

Mykel Stone

  • NA
  • 13
  • 0

C#, Bitmaps and DPI, and Printing Woes

Feb 22 2008 4:25 PM
I'm writing an application, who's main purpose is to collect some end user input, format it and send it to the default printer. To do this task, I am drawing my content onto a bitmap, then pasting that bitmap onto a print document, then printing it. While that works fine, we want the printouts to be a bit sharper, increasing the DPI seemed to be the trick. However, when the DPI is set to 512 and above, the printout becomes blank for some reason, despite being fine on anything lower. Why is this? I can't seem to discover the answer. I've posted the code below. Thanks in advance!

namespace GCW
{
    public class f_print : Form
    {       
        public f_print()
        {    
        }

        Graphics gfx; // Our graphics device, we use this to draw the text
        Bitmap printmap = new Bitmap(850, 1100); // Our bitmap, 100 = 1 inch, 8.5'x11' sized paper
       
        MarginsBox m = new MarginsBox(); // Margins, keeps our stuff nice and tidy
        Font f; // Font object, to store fonts
        PrintDocument pd = new PrintDocument(); // Red line to the printer

        float dpi = 128; // Dots Per Inch
        public void Start()
        {
            printmap.SetResolution(dpi, dpi); // Set the resolution of our paper
            m.Top = 1 * dpi; // Set a 1' margin, from the top
            m.Left = 1.25f * dpi; // Set a 1.25' margin, from the left
            m.Bottom = printmap.Height - m.Top; // 1', from the bottom
            m.Right = printmap.Width - m.Left; // 1.25', from the right
            m.Width = printmap.Width - (m.Left * 2); // Get the width of our working area
            m.Height = printmap.Height - (m.Top * 2); // Get the height of our working area

            gfx = Graphics.FromImage(printmap); // Transfer the bitmap into canvas
            f = new Font("Verdana", 12); // set the font to Verdana, 12
            gfx.DrawString("Printing Test; Printing Test; Printing Test; Printing Test;", f, Brushes.Black, new RectangleF(m.Left, m.Top, m.Width, m.Height)); // Our text, put it on the canvas
           

            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); // Set the page event handler for printing, that way we can do things before this happens
            pd.DefaultPageSettings.Color = true; // We want to print in color
            pd.Print(); // Print

            try
            {
                this.FormBorderStyle = FormBorderStyle.Fixed3D;
                this.Name = "frmPrint"; // Our form name
                this.Text = "GCW - Print";
                this.MaximizeBox = false; // We can't maximize
                this.Size = new Size(400, 400); // Form size               
                this.Show(); // Show the form
            }
            catch (ObjectDisposedException)
            {
            }
        }

    // Our printing event
        public void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImageUnscaled(printmap, new Point(0, 0)); // Draw our paper
        }
   
    //This is pretty self explanitory.
        struct MarginsBox
        {
            public float Top;
            public float Left;
            public float Bottom;
            public float Right;
            public float Width;
            public float Height;
        }
    }
}


Answers (2)