0
Answer

How to retrieve the printer's device context in C#

Hasan

Hasan

16y
2.4k
1
Hi,

I am working with C#. In my project I need to take printout of a form in report fashon. This form comprises of pictures, texts, graphs and other controls. So I have decided to take the print out from the screenshot of the form. For doing that I have used bitblt() function first. But the printing quality with that is really very poor. So I have decided to use stretchblt() function for getting a better quality.

Now I need to take the device context of the available printer first for using the stretchblt() function successfully. But the usual getprinterdc() function (supported in MFC) is not working in C#. So plz tell me how can I solve this problem. I am using the following code:

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

public static extern int SetStretchBltMode(IntPtr hdcDest, int nStretchMode);

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

public static extern int StretchBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int nWidthSrc, int nHeightSrc, System.Int32 dwRop);

..

..

..

private void CaptureScreen()

{

Graphics mygraphics = this.CreateGraphics();

..

..

IntPtr dc1 = mygraphics.GetHdc(); //Source DC

IntPtr dc2 = here I have to retrieve the available Printer DC as my destination DC  //Target DC

SetStretchBltMode(dc2, 4); //Here 4 is defined for HALFTONE

StretchBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height - 75, dc1, 0, 75, this.ClientRectangle.Width, this.ClientRectangle.Height, 13369376); //Here 13369376 is defined as SRCCOPY

..

..

..

//Saving in a memory stream

memoryImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

}

 

Plz also let me know if u know any better solution of this problem

Thanks in advance