Click here to Skip to main content
14,971,448 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi i have write a little signaturepad with panel.
I can not understand where is my error.
on desktop pc with high resolution the screenshot of the signature will be saved correctly from teh panel (panel1).
on pc with low resolution the screenshot wil be come from any place on the desktop.


C#
    private void buttonInsert_Click(object sender, EventArgs e)
{
    Image bmp = new Bitmap(panel1.Width, panel1.Height);

    var gg = Graphics.FromImage(bmp);
    var rect = panel1.RectangleToScreen(panel1.ClientRectangle);

    gg.CopyFromScreen(rect.Location, Point.Empty, panel1.Size);
    bmp.Save(@"C:\Temp\test.jpg", ImageFormat.Jpeg);

    Graphics g = panel1.CreateGraphics();
    g.Clear(Color.WhiteSmoke);
}


What I have tried:

i tried everything but i cannot understand where is the error, i am sorry i am a newby
I must use
CopyFromScreen
, otherway the panel remains white.
thx
Posted
Updated 21-Jun-21 19:56pm

Check out the following code. It creates 3 graphics (PNGs).
1. using your original code
2. using PointToClient() conversion method of Form object
3. using PointToScreen() conversion method of Form object

The first two get the same rectangle area on my computer but the last one is shifted. Maybe you need to convert PointToScreen? Take a look at coords more closely and you should be able to figure it out. Run the code and compare what you get on different resolutions.

FYI -Mine was 1280 x 960 on a remote VM.

C#
Image bmp = new Bitmap(panel1.Width, panel1.Height);
            
var gg = Graphics.FromImage(bmp);

var rect = panel1.RectangleToScreen(panel1.ClientRectangle);

gg.CopyFromScreen(rect.Location, Point.Empty, panel1.Size);
bmp.Save(@"C:\temp\test.jpg", ImageFormat.Jpeg);
            

Point p = this.PointToClient(new Point(panel1.ClientRectangle.X, panel1.ClientRectangle.Y));
            Point p2 = this.PointToScreen(new Point(panel1.ClientRectangle.X, panel1.ClientRectangle.Y));
            Size s = new Size(panel1.Width,panel1.Height);
           
Rectangle targetRect = new Rectangle(p,s);
Rectangle targetRect2 = new Rectangle(p2, s);
var rect1 = panel1.RectangleToScreen( targetRect );
var rect2 = panel1.RectangleToScreen(targetRect2);


gg.CopyFromScreen(targetRect.Location, Point.Empty, s);
bmp.Save(@"C:\temp\test1.jpg", ImageFormat.Jpeg);

gg.CopyFromScreen(targetRect2.Location, Point.Empty, s);
bmp.Save(@"C:\temp\test2.jpg", ImageFormat.Jpeg);

Graphics g = panel1.CreateGraphics();
            
g.Clear(Color.WhiteSmoke);
   
I found it out, the problem was the windows scaling. When i set it on 100% the Problem solved.
thx a lot
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900