1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
public partial class Form1 : Form
{
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int
nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
private static Image PageAImprimer;
//A method to draw a Form to an image.
private void DrawToBitmap(Control f, System.Drawing.Bitmap image)
{
System.Drawing.Graphics g = f.CreateGraphics();
System.Drawing.Graphics g2 = System.Drawing.Graphics.FromImage(image);
IntPtr gi = g.GetHdc();
IntPtr gi2 = g2.GetHdc();
BitBlt(gi2, 0, 0, f.Width, f.Height, gi, 0, 0, 0x00CC0020);
g.ReleaseHdc();
g2.ReleaseHdc();
g.Dispose();
g2.Dispose();
}
public WebBrowser previewBrowser { get; set; }
public Form1()
{
InitializeComponent();
previewBrowser = new WebBrowser();
previewBrowser.Url = new Uri("http://www.google.fr");
previewBrowser.Update();
}
private void BtImprimer_Click(object sender, EventArgs e)
{
// Imprimante par défaut
printDocument1.Print();
// Second imprimante
printDocument1.PrinterSettings.PrinterName = "\\\\imps\\otherPrinter";
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap targetImage = new Bitmap(previewBrowser.Width, previewBrowser.Height);
DrawToBitmap(previewBrowser, targetImage);
e.Graphics.DrawImage(targetImage, new Rectangle(0,0, previewBrowser.Width, previewBrowser.Height));
}
}
} |
Partager