Question

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.

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top