Hi, i having a problem in C# printing method
Let Say, I have C# Form1 and Form2
Form1 had a button on it, it says Print Page Form2
Form2 didnt had anything on design or had anything on design
My objective was,
to print page Form2
I searching on the web Microsoft and Others, i finding the answer like this
Code:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printButton_Click(object sender, EventArgs e)
{
CaptureScreenForm();
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(NewForm.memoryImageSender, 0, 0);
}
Thats whole code for printing the Form1.
But its not My Objective, so forget it
This is what i have in Form2
Code:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
Bitmap memoryImage;
public void CaptureScreenForm2()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
public Bitmap memoryImageSender
{
get
{
return memoryImage;
}
}
And so i call the Form2 in Form1 like this
Code:
public Form1()
{
InitializeComponent();
NewForm = new Form2();
}
private void printButton_Click(object sender, EventArgs e)
{
NewForm.CaptureScreenForm2(); //Calling The Form2 CaptureScreen Method
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(NewForm.memoryImageSender, 0, 0); //Calling The Form2 MemoryImage Bitmap
}
And my idea for this is failure, nothing printed
Does Anyone have idea to overcoming this problem? I already dont have any idea.