Jag gjorde så här:
RectangleF GetScaledRectangle(float x, float y, RectangleF r)
{
RectangleF result = new RectangleF(r.X * x, r.Y * y, r.Width * x, r.Height * y);
return result;
}
private void DrawAll(Graphics g)
{
RectangleF srcRect = new Rectangle(0, 0, this.panel1.Width,
panel1.Height);
int nWidth = 802;
int nHeight = 1104;
RectangleF destRect = new Rectangle(0, 0, nWidth, nHeight);
float scalex = destRect.Width/this.panel1.Width;
float scaley = destRect.Height/this.panel1.Height;
GraphicsUnit gu = GraphicsUnit.Pixel;
RectangleF scaledRectangle;
Brush br = new System.Drawing.SolidBrush(Color.FromArgb(((System.Byte)(74)), ((System.Byte)(74)), ((System.Byte)(74))));
scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox1.Bounds);
Image my1Image = (Image)pictureBox1.Image.Clone();
g.DrawImage(my1Image, scaledRectangle,pictureBox1.Image.GetBounds(ref gu), GraphicsUnit.Pixel);
scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox2.Bounds);
Image my2Image = (Image)pictureBox2.Image.Clone();
g.DrawImage(my2Image, scaledRectangle,pictureBox2.Image.GetBounds(ref gu), GraphicsUnit.Pixel);
scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox3.Bounds);
Image my3Image = (Image)pictureBox3.Image.Clone();
g.DrawImage(my3Image, scaledRectangle,pictureBox3.Image.GetBounds(ref gu), GraphicsUnit.Pixel);
if(pictureBox4.Visible && pictureBox5.Visible)
{
scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox4.Bounds);
Image my4Image = (Image)pictureBox4.Image.Clone();
g.DrawImage(my4Image, scaledRectangle,pictureBox4.Image.GetBounds(ref gu), GraphicsUnit.Pixel);
scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox5.Bounds);
Image my5Image = (Image)pictureBox5.Image.Clone();
g.DrawImage(my5Image, scaledRectangle,pictureBox5.Image.GetBounds(ref gu), GraphicsUnit.Pixel);
}
foreach(Control x in this.panel1.Controls)
{
if(x.GetType().ToString() == "System.Windows.Forms.Label" && x.Visible)
{
Label theText = (Label)x;
x.BringToFront();
StringFormat drawFormat = new StringFormat();
if(theText.TextAlign.ToString() == "TopRight")
{
drawFormat.Alignment = StringAlignment.Far;
g.DrawString(theText.Text, theText.Font,br , (theText.Bounds.Left+theText.Width+2)*scalex, theText.Bounds.Top * scaley, drawFormat);
}
else if(theText.TextAlign.ToString() == "TopCenter")
{
drawFormat.Alignment = StringAlignment.Center;
g.DrawString(theText.Text, theText.Font,br , (theText.Bounds.Left+(theText.Width/2))*scalex, theText.Bounds.Top * scaley, drawFormat);
}
else
g.DrawString(theText.Text, theText.Font,br , theText.Bounds.Left*scalex, theText.Bounds.Top * scaley, drawFormat);
}
}
}
private void SkrivUt(int antal)
{
printDocument1.DefaultPageSettings.Margins.Left = 0;
printDocument1.DefaultPageSettings.Margins.Right = 0;
printDocument1.DefaultPageSettings.Margins.Top = 0;
printDocument1.DefaultPageSettings.Margins.Bottom = 0;
printDialog1.Document = this.printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
this.printDocument1.Print();
}
}
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
this.panel1.Focus();
DrawAll(e.Graphics);
}
Jag hade först en loop på alla pitureboxar men eftersom den alltid missade en av dom(växlade mellan 2 olika boxar) så la jag dom en och en.
Eftersom jag dessutom hade labels placerade på pictureboxarna så var jag tvungen med ytterligare en loop för alla labels för att inte en picturebox skulle skriva över en massa labels.