C
C#2y ago
fireshaper

❔ Printing text using e.Graphics and label bounds

I'm trying to print what is shown on a winform and realized that using a bitmap just wouldn't give me the results I needed, and that I'd need to use e.Graphics to just manually draw everything. While time consuming, it's not a huge deal since it's just a lot of similarly named labels. The problem I'm having is that label text isn't being put in the correct place when I use label.bounds.location. I've attached a partial screenshot of what I see in the form and also what I'm seeing in the print preview. Also, here's the code I'm using:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//e.Graphics.DrawImage(memoryImage, e.MarginBounds);

Pen blackPen = new Pen(Color.Black);
Brush blackBrush = new SolidBrush(Color.Black);

e.Graphics.DrawRectangle(blackPen, rabbitBoxViewRoot.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSSSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSSDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSDSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSDDam.Bounds);

e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDSSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDSDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDDSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDDDam.Bounds);

e.Graphics.DrawString(lblFarmNameRoot.Text, lblFarmNameRoot.Font, blackBrush, lblFarmNameRoot.Bounds);
e.Graphics.DrawString(lblRabbitNameRoot.Text, lblRabbitNameRoot.Font, blackBrush, lblRabbitNameRoot.Bounds.Location);
e.Graphics.DrawString(lblEarNumRoot.Text, lblEarNumRoot.Font, blackBrush, lblEarNumRoot.Bounds.Location);
e.Graphics.DrawString(lblColorRoot.Text, lblColorRoot.Font, blackBrush, lblColorRoot.Bounds.Location);
e.Graphics.DrawString(lblRegNumRoot.Text, lblRegNumRoot.Font, blackBrush, lblRegNumRoot.Bounds.Location);
e.Graphics.DrawString(lblGCNumRoot.Text, lblGCNumRoot.Font, blackBrush, lblGCNumRoot.Bounds.Location);
e.Graphics.DrawString(lblSexRoot.Text, lblSexRoot.Font, blackBrush, lblSexRoot.Bounds.Location);
e.Graphics.DrawString(lblBirthdayRoot.Text, lblBirthdayRoot.Font, blackBrush, lblBirthdayRoot.Bounds.Location);
e.Graphics.DrawString(lblWeightRoot.Text, lblWeightRoot.Font, blackBrush, lblWeightRoot.Bounds.Location);
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//e.Graphics.DrawImage(memoryImage, e.MarginBounds);

Pen blackPen = new Pen(Color.Black);
Brush blackBrush = new SolidBrush(Color.Black);

e.Graphics.DrawRectangle(blackPen, rabbitBoxViewRoot.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSSSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSSDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSDSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewSDDam.Bounds);

e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDSSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDSDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDDam.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDDSire.Bounds);
e.Graphics.DrawRectangle(blackPen, rabbitBoxViewDDDam.Bounds);

e.Graphics.DrawString(lblFarmNameRoot.Text, lblFarmNameRoot.Font, blackBrush, lblFarmNameRoot.Bounds);
e.Graphics.DrawString(lblRabbitNameRoot.Text, lblRabbitNameRoot.Font, blackBrush, lblRabbitNameRoot.Bounds.Location);
e.Graphics.DrawString(lblEarNumRoot.Text, lblEarNumRoot.Font, blackBrush, lblEarNumRoot.Bounds.Location);
e.Graphics.DrawString(lblColorRoot.Text, lblColorRoot.Font, blackBrush, lblColorRoot.Bounds.Location);
e.Graphics.DrawString(lblRegNumRoot.Text, lblRegNumRoot.Font, blackBrush, lblRegNumRoot.Bounds.Location);
e.Graphics.DrawString(lblGCNumRoot.Text, lblGCNumRoot.Font, blackBrush, lblGCNumRoot.Bounds.Location);
e.Graphics.DrawString(lblSexRoot.Text, lblSexRoot.Font, blackBrush, lblSexRoot.Bounds.Location);
e.Graphics.DrawString(lblBirthdayRoot.Text, lblBirthdayRoot.Font, blackBrush, lblBirthdayRoot.Bounds.Location);
e.Graphics.DrawString(lblWeightRoot.Text, lblWeightRoot.Font, blackBrush, lblWeightRoot.Bounds.Location);
}
14 Replies
HimmDawg
HimmDawg2y ago
Have you tried to debug cour code? Maybe the location is set to (0, 0) somewhere
fireshaper
fireshaperOP2y ago
good Idea
fireshaper
fireshaperOP2y ago
Yeah, it's not very far from where it's displaying
fireshaper
fireshaperOP2y ago
compared to the location of the box
HimmDawg
HimmDawg2y ago
That is because the location of the label is relative to the box
fireshaper
fireshaperOP2y ago
aha! That makes sense so I should do the box's bounds + the label
HimmDawg
HimmDawg2y ago
Yeah that'd work
fireshaper
fireshaperOP2y ago
that looks a lot better
fireshaper
fireshaperOP2y ago
Thanks Is there a way to use e.MarginBounds when drawing rectangles and text manually? My form is apparently larger than the print area
fireshaper
fireshaperOP2y ago
fireshaper
fireshaperOP2y ago
HimmDawg
HimmDawg2y ago
Not sure how MarginBounds would help fluffyFoxLurk you have to somehow project the image you wanna print to the print area
fireshaper
fireshaperOP2y ago
When I was just doing a bitmap of the screen I could use e.MarginBounds in the DrawImage to tell it to print to the bounds of the page But I guess that's not an option since I'm manually drawing everything now
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server