When I print bitmap, it eats up some of it - c#

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.

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);

Related

Print out All images from bitmap Array in C#

I've been trying to print Bitmap Array image in C# (each index image should print separate page)
The length of the array is defined while running the program
Bitmap creating code:
Bitmap[] bitm = new Bitmap[dataGridView1.RowCount]
This code I used to call print page event:
bitm[j].Save(#"Image/" + dataGridView2.Rows[j].Cells[0].Value.ToString() + "Term Report.jpg", ImageFormat.Jpeg);
pictureBox2.Image = bitm[j];
pictureBox2.Width = bitm[j].Width;
pictureBox2.Height = bitm[j].Height;
myPrintDocument2.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
myPrinDialog2.Document = myPrintDocument2;
if (myPrinDialog2.ShowDialog() == DialogResult.OK)
{
myPrintDocument2.Print();
}
Print document's print page event:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
pictureBox2.DrawToBitmap(myBitmap2, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height));
e.Graphics.DrawImage(myBitmap2, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height));
}
But it is displaying printdialog for each and every page. But I need all pages should print one printdialog!

Getting blank print document on button click using C# Windows application

I'm trying to print panel contents in C# windows application on button click but, I'm getting blank document have attached some screenshots and code.
My Code:
private void BtnPrint_Click(object sender, EventArgs e)
{
Bitmap bitmap;
//Add a Panel control.
Panel panel = new Panel();
this.Controls.Add(panel);
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}

Draw Borders in Print Preview but the Borders Should not Print C#

I'm using WinForms. In my Form i have a picturebox and a button that prints the pictures in the picturebox.
In my code when you click on the print button the program displays a print preview with a rectangular box around the image. I drew this rectangular box because I have specific kinds of papers that i print to. These papers have pictures on the borders. The users cant print over the pictures on the paper. I just want to inform the users that if you pass these rectangular borderlines in the print preview that you will be printing on top of the pictures on the paper.
Goal: When i click on the print button I want to see a print-preview paper with a rectangular border, but i don't want the rectangular boarder to print. I just want the image to print.
private void button1_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
var bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.DrawToBitmap(bmp, this.pictureBox1.ClientRectangle);
e.Graphics.DrawRectangle(Pens.Salmon, 25, 25, 500, 1000);
e.Graphics.DrawImage(bmp, 25, 25, 500, 500); //Gets the input from the textboxes
}
You can use the PrintController.IsPreview property for that:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
var bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.DrawToBitmap(bmp, this.pictureBox1.ClientRectangle);
if (this.printDocument1.PrintController.IsPreview) {
e.Graphics.DrawRectangle(Pens.Salmon, 25, 25, 500, 1000);
}
e.Graphics.DrawImage(bmp, 25, 25, 500, 500); //Gets the input from the textboxes
}

Printing does not start at the top edge of the page

I am trying to print some strings using Graphicss.DrawString(). I have set margins to the printdocument but does not start from the origin of the page. I have set margins to (0,0,0,0) but somehow it prints half centimeter below the top edge of the page. Another thing is that it can print from left edge.
Below is my code.
private void button1_Click(object sender, EventArgs e)
{
////PaperSize pkCustomSize1 = new PaperSize("First custom size", 1020, 3517);
////printDocument1.DefaultPageSettings.PaperSize = pkCustomSize1;
printPreviewDialog1.Document = printDocument1;
printDocument1.PrinterSettings.PrinterName = this.comboBox1.Text;
Margins margins = new Margins(0, 0, 0, 0);
printDocument1.PrinterSettings.DefaultPageSettings.Margins = margins;
printPreviewDialog1.Show();
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int resX = GetPrinterResolutionX(comboBox1);
int resY = PrnOpra.GetPrinterResolutionY(comboBox1);
Graphics g = e.Graphics;
float scale = resX / ScrnRes;
Bitmap bm = new Bitmap(367, 1205);
g.DrawRectangle(new Pen(Color.Black, 0.5F), panel9.Location.X / 2, panel9.Location.Y / 2, panel9.Width, panel9.Height);
g.DrawImage(bm, 0, 0);
}
What's wrong with code?
You have to set the PrintDocument.OriginAtMargins property to true to consider your margins.
From MSDN,
When OriginAtMargins is true, the Graphics object location takes into account the PageSettings.Margins property value and the printable area of the page
But printing from the exact edge depends on the printable area which is defined by the physical limitations of the printing device. Check the HardMarginX and HardMarginY to get the physical origin of the printer. For more information refer the answer of this question.

How can I save my drawings(such as rectangle, circle) from panel into image?

How can I save my drawings(such as rectangle,circle) from panel into image?
I have tried this code but I don't know why it only gives me white image always:
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = "bmp";
saveFileDialog.Filter = "Bitmap files|*.bmp";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
int width = panel1.Width;
int height = panel1.Height;
Bitmap bitMap = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bitMap, new Rectangle(0, 0, panel1.Width, panel1.Height));
bitMap.Save(saveFileDialog.FileName);
}
Do not use CreateGraphics for drawing your graphic, since that is only a temporary drawing (it will get erased by other windows or if you minimize the the form, etc).
Use the panel's paint event to do your drawing:
panel1.Paint += panel1_Paint;
void panel1_Paint(object sender, PaintEventArgs e) {
// draw stuff with e.Graphics
}
call the panel's Invalidate method to make the paint method get called again.

Categories

Resources