views:

517

answers:

2

I have some code that is used to programmatically create a Document to send to the printer. It goes something like this:

private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev) 
{      
      ev.Graphics.DrawImage(pictureBox1.Image, 50, 100);

      string drawToday="Date      : "+strToday;
      string drawPolicyNo="Policy # : " + strPolicyNo;
      string drawUser="User     : " + strUser;
      Font drawFont=new Font("Arial",30);
      SolidBrush drawBrush=new SolidBrush(Color.Black);
      PointF drawPointToday=new Point(50,400);
      PointF drawPointPolicyNo=new Point(50,450);
      PointF drawPointUser=new Point(50,500);
      ev.Graphics.DrawString(drawToday,drawFont,drawBrush,drawPointToday);
      ev.Graphics.DrawString(drawPolicyNo,drawFont,drawBrush,drawPointPolicyNo);
      ev.Graphics.DrawString(drawUser,drawFont,drawBrush,drawPointUser);
}

Its effective code, but now I need to do the same procedure but instead write it to an image file so that it can be sent to a browser and printed from there. It should be relatively simple to reuse this code, but I am getting hung up, unfortunately, on what drawing surface to use in replacement of the PrintPageEventArgument.

Thanks

Edit: THanks I get that I just need another Graphics object, but the Graphics object by itself does not have a public constructor, so what I am looking for is a suggestion on what object I need to substatiate to be able to create a Graphics object to draw on. I thought perhaps bitmap? Bitmaps are of course pixel based instead of point based so I was not sure that this is the best medium to use.

+4  A: 

If I understand you correctly you could simply break out the code from your event handler into another method accepting a Graphics object as parameter:

private void Print(Graphics g)
{
    g.DrawImage(pictureBox1.Image, 50, 100);

    string drawToday="Date      : "+strToday;
    string drawPolicyNo="Policy # : " + strPolicyNo;
    string drawUser="User     : " + strUser;
    Font drawFont=new Font("Arial",30);
    SolidBrush drawBrush=new SolidBrush(Color.Black);
    PointF drawPointToday=new Point(50,400);
    PointF drawPointPolicyNo=new Point(50,450);
    PointF drawPointUser=new Point(50,500);
    g.DrawString(drawToday,drawFont,drawBrush,drawPointToday);
    g.DrawString(drawPolicyNo,drawFont,drawBrush,drawPointPolicyNo);
    g.DrawString(drawUser,drawFont,drawBrush,drawPointUser);
}

And then call that method from your event handler:

private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev) 
{      
      Print(ev.Graphics);
}

Then you can reuse the same method for printing the output to any other Graphics instance:

using (Bitmap img = new Bitmap(width, height))
using (Graphics g = Graphics.FromImage(img))
{
    Print(g);
    img.Save(fileName);
}
Fredrik Mörk
Thanks Fredrik, kinda what I figured. Bitmap would be the way to go on this.
Goblyn27
You are welcome. Also note heavyd's answer on image formats, in case you want more control over format, color depth and such.
Fredrik Mörk
+2  A: 

To write your graphics object to an image file you can do something like this:

public void SaveImage(Graphics surface)
{
  Bitmap bmp = new Bitmap(50, 100, surface);
  bmp.Save("filename.png", ImageFormat.Png);
}

You can choose other formats from the ImageFormat class such as JPG, BMP, etc.

heavyd