Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CopyFromScreen not working

Tags:

c#

This one is slightly confusing...

I am using Adobe's PDF Viewer control to view PDFs but I want the user to be able to drag an image onto the PDF and then when they click save it adds the image to the PDF at that location.

Implementing the PDF viewer proved quite difficult but I decided in the end to use Adobe's control, take a picture and then allow the user to draw the image ontop of the picture of the PDF. When they click save I am going to use PDFSharp to put the image onto the PDF once I've worked out where it goes but the problem I have at the moment is that I can't get a picture of the PDF.

The following code is used to get the picture but the Panel that it is attached to just appears with a white background with a red 'X' and border...

using (Bitmap bitmap = new Bitmap(adobePDFViewer1.Width, adobePDFViewer1.Height))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        g.CopyFromScreen(new Point(adobePDFViewer1.Left, adobePDFViewer1.Top), Point.Empty, adobePDFViewer1.Size);
                    }
                    panelOverPdfViewer.BackgroundImage = bitmap;
                }

I don't think this is the best way of doing it but I couldn't work out any others. Any help would be appreciated!

EDIT:

Following a very helpful answer below this is working code:

Here is the code I used:

Bitmap printscreen = new Bitmap(adobePDFViewer1.Width, adobePDFViewer1.Height);
                Graphics graphics = Graphics.FromImage(printscreen as Image);
                int left = this.Left + 396;
                int top = this.Top + 30;
                graphics.CopyFromScreen(left, top, 0, 0, printscreen.Size);
                pictureBoxOverPDFView.Image = printscreen;
like image 683
Compunutter Avatar asked Feb 06 '26 13:02

Compunutter


1 Answers

Look at this this Print-Screen

and try this for test work of CopyFromScreen

private void PrintScreen()

{  

Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

Graphics graphics = Graphics.FromImage(printscreen as Image);

graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);

printscreen.Save(@"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);

} 
like image 112
Likurg Avatar answered Feb 08 '26 04:02

Likurg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!