3

In my application i have a chart which is surrounded inside a panel. I added a printdocument component from the toolbox and when i want to print the chart, i am creating a bitmap and i get the panel inside the bitmap (and as a result the chart which is inside the panel). My problem is that when i create the bitmap, when i send it to the printer to print it, it eats up some of the chart from the bottom and from the right side. Below is my code to execute this

private void button1_Click(object sender, EventArgs e)
    {
        PrintDialog printDialog = new PrintDialog();
        printDialog.Document = printDocument1;
        printDialog.UseEXDialog = true;
        //Get the document

        if (DialogResult.OK == printDialog.ShowDialog())
        {
            printDocument1.DocumentName = "Test Page Print";
            printPreviewDialog1.Document = printDocument1;

            if (DialogResult.OK == printPreviewDialog1.ShowDialog())
            printDocument1.Print();
        }
    }

This is the button to initialize the print_document. (I added a print preview so that i don't have to print it every time and spent paper and ink)

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Bitmap bm = new Bitmap(this.panel1.Width, this.panel1.Height);
        this.panel1.DrawToBitmap(bm, new Rectangle(50, 50, this.panel1.Width + 50, this.panel1.Height + 50));
        e.Graphics.DrawImage(bm, 0, 0);
    }

I was thinking that maybe the chart is too big and it doesn't fit in the page but if i save the bitmap. it fits OK inside the page actually there is too much free space on the bottom and right side. (After the draw to bitmap function add

bm.Save(@"c:\LOAN_GRAPH.png");

instead of the draw image and the image is save in c:)

Anybody who can help me i would be truly thankful.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
George Georgiou
  • 455
  • 2
  • 14
  • 27
  • So if the generated bitmap looks fine when you save it to the disk, have you tried then sending that image to the printer? To me it sounds more like the print process adjusting the image to fit something, and its cropping the rest of the image. – cDecker32 May 25 '12 at 16:49
  • What if you try this overload? e.Graphics.DrawImage(bm, new Rectangle(Point.Empty, bm.Size), new Rectangle(Point.Empty, bm.Size), GraphicsUnit.Pixel); – Dan Byström May 25 '12 at 16:50
  • Its working. I removed the panel and instead, i am creating the rectangle according to my chart. Bitmap bm = new Bitmap(this.chart1.Width, this.chart1.Height); this.chart1.DrawToBitmap(bm, new Rectangle(50, 50, this.chart1.Width + 50, this.chart1.Height + 50)); e.Graphics.DrawImage(bm, 0, 0); and its working like a charm – George Georgiou May 25 '12 at 17:12

1 Answers1

1

Its working. I removed the panel and instead, i am creating the rectangle according to my chart.

Bitmap bm = new Bitmap(this.chart1.Width, this.chart1.Height);
    this.chart1.DrawToBitmap(bm, new Rectangle(50, 50, this.chart1.Width + 50, this.chart1.Height + 50));
    e.Graphics.DrawImage(bm, 0, 0);
George Georgiou
  • 455
  • 2
  • 14
  • 27