Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
I have a problem: I need to capture of Form to image file in C#. This is my code to do this, but it is not to work correctly, I want to get only client area, not get another area of form:
public void GhiImage()
        {
            try
            {
                SaveFileDialog SvDialog = new SaveFileDialog();
                SvDialog.Title = "Ghi thanh file anh!";
                SvDialog.DefaultExt = "bmp";
                SvDialog.Filter = "Ảnh bitmap (*.bmp)|*.bmp |Ảnh jpg (*.jpg)|*.jpg";
                SvDialog.FilterIndex = 1;

                if (SvDialog.ShowDialog() == DialogResult.OK)
                {
                    Graphics g = this.CreateGraphics();
                    Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
                    this.DrawToBitmap(bmp, this.ClientRectangle);
                    bmp.Save(SvDialog.FileName);
                    bmp.Dispose();
                }
            }
            catch
            {
            }
        }


It contains all of Form (border and client area). Now help me fix my code to work exactly. Thanks you, everyone.

What I have tried:

I coded it, but it worked uncorrectly!
Posted
Updated 20-Sep-18 0:15am
v2

1 solution

Both these lines:
C#
Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
this.DrawToBitmap(bmp, this.ClientRectangle);
allow you to specify which part of the client area you want to draw to your bitmap. Here it does exactly want you asked for: it draws the entire client area. If you want only a portion of it, you have to define a rectangle delimiting this portion, and use this rectangle instead of ClientRectangle.
 
Share this answer
 
Comments
Volga_ 20-Sep-18 6:22am    
Thanks for your shearing. But I want to get only client area, my code above gets all client area and border area of form. I can not do more for this problem, help me with code. Thanks.
phil.o 20-Sep-18 6:37am    
You know the moto "Give a man a fish, etc..."? When you say you cannot do more, I actually read I don't want to understand, just be provided with a ready made solution.
Please have a look at Rectangle.Inflate method [^], which should allow you to adapt rectangle's dimensions to your needs.
Volga_ 20-Sep-18 7:36am    
Thanks. Maybe my English is very bad, and you do not understand my problem. Thanks again.
phil.o 20-Sep-18 7:46am    
I understood your problem: you want to strip off the borders from the image. Problem is: I do not have any clue about the dimensions of these borders. So, how am I supposed to be more acurate? It is up to you to use the method I gave you a link for, so that you can find yourself the way to achieve your goal.
I do not thank you for the downvote, though.
Volga_ 20-Sep-18 8:09am    
Because of your hidden reply I did not understand all and voted down with your reply. Sorry you so much, I am new C#, I am mechanical engineer, not am IT engineer, so that everything is difficult for me. I will be studying things that you sent to me and hope that it is good for me. Problem about the dimensions of these borders I thinked and calculated: top=20, bottom=8, left=8 and right=8. But I do not understand why above my code was not to work as I want. If you can, you explain with code. I am not the same person in the motoes "Give a man a fish..." as you quoted.

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