PictureBox to Print Page...

V

VJ

I have a sample code.. below with my Print problem... I am not able to get
the Label on Image print at the right location..
VJ

private Label label4 = new Label();

Form1's Load...

label4.BackColor=Color.White;
label4.BorderStyle=BorderStyle.FixedSingle;
label4.Text = "label4";
label4.Location=new Point(300,1450);
label4.Size = new Size(50,50);
pictureBox1.Controls.Add(label4);

// pictureBox1 is a control on Form1, inisde Panel1
// with a large enough image to have scroll bars...
// the picturebox1.SizeMode = SizeMode.Normal


Button Click Event

PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage+=new PrintPageEventHandler(printDoc_PrintPage);

PrintPreviewDialog printPreviewDlg = new PrintPreviewDialog();
printPreviewDlg.Document = printDoc;
printPreviewDlg.ShowDialog();

private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{

Bitmap imageForPrinting = new Bitmap(pictureBox1.Width,
pictureBox1.Height);
using (Graphics graphicsImage = Graphics.FromImage(imageForPrinting))
using (DibGraphicsBuffer dib = new DibGraphicsBuffer())
// Create temporary screen graphics
using (Graphics graphicsForm = this.CreateGraphics())
// Create temporary graphics from the Device Independent Bitmap
using (Graphics graphicsTemp =
dib.RequestBuffer(graphicsForm, e.PageBounds.Width,
e.PageBounds.Height))
{


graphicsTemp.DrawImage(pictureBox1.Image,
e.PageBounds,
0, 0, pictureBox1.Width, pictureBox1.Height,
GraphicsUnit.Pixel);


// The below piece of code is not printing.. on the Print Documents.. I
know
// we have to transform points.. but How do we do that???

PointF labelLocation = new PointF(label3.Left, label3.Top);
graphicsTemp.DrawString(label3.Text, label3.Font,
new SolidBrush(Color.Black), labelLocation);


// Use the buffer to paint onto the final image
dib.PaintBuffer(graphicsImage, 0, 0);
// Draw this image onto the printer graphics,
// adjusting for margins
e.Graphics.DrawImage(imageForPrinting,
e.MarginBounds.Left, e.MarginBounds.Top);

}

}
 
V

VJ

Ok sorry people... that was a momentary loss of Math Mindset......I got
it... just add these lines instead of the existing text printing...

PointF ptAtt = new PointF(
((float)e.PageBounds.Width/(float)pictureBox1.Width)
, ((float)e.PageBounds.Height/(float)pictureBox1.Height) );

PointF labelLocation = new PointF(ptAtt.X * (float)label4.Left, ptAtt.Y
* (float)label4.Top);
graphicsTemp.DrawString(label4.Text, label4.Font,
new SolidBrush(Color.Black), labelLocation);

VJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top