Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a panel, I can copy/capture all its surface to a bitmap by using the method panel.DrawToBitmap() but I just want to copy/capture only a specified rectangle on that panel to a bitmap, could you please give me some solution to achieve this?

Your help would be highly appreciated!
Thanks!
Posted

1 solution

Here is another easy solution i have figured out:
C#
Bitmap bp=new Bitmap(dataGridView1.Width,dataGridView1.Height);
dataGridView1.DrawToBitmap(bp,new Rectangle(0,0,dataGridView1.Width,dataGridView1.Height));
Bitmap bp1 = bp.Clone(new Rectangle(50, 50, 100, 100),  System.Drawing.Imaging.PixelFormat.DontCare);
 bp1.Save("test2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

the cords of rectangle passed in second bitmap will crop the complete image to ur required size and u will have only cropped portion u needed.
 
Share this answer
 
v2
Comments
supernorb 15-Feb-13 8:20am    
I don't think the second argument is the rectangle calculated on the panel, it is calculated on Bitmap object (the first argument) and I tried this first, of course it didn't work as I expected! However I tried another method, first create the Bitmap, then obtain a Graphics object from that Bitmap (through the static method Graphics.FromImage), then call the method CopyFromScreen of this Graphics object to Copy/capture the specified rectangle on the panel, because the coordinates are in screen, the coordinates on the panel should be converted to the coordinates on the screen by using the method PointToScreen()... This works well but I still want to find another solution if any one exists. Thanks!
Ag_Sharad 15-Feb-13 9:32am    
Dear i have just tried it. The rectangle parameter of DrawToBimap() takes values in relation to the control on which it is called. The x,y values of the rectangle takes its origin from controls's top left corner. See the following code:

Bitmap bp=new Bitmap(100,100);
dataGridView1.DrawToBitmap(bp,new Rectangle(0,0,100,100));
bp.Save("test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

the test.jpg contains the 100 by 100 pixel image of the datagridview from top left corner. It would work the same for all other controls.....
supernorb 15-Feb-13 9:59am    
Your sample couldn't reflect the truth, you can try creating a new Bitmap with the size being the same to dataGridView1's size and passing the second argument as a Rectangle of (50,50,100,100) (of course the width and height of dataGridView1 should be greater than 150), then show the Bitmap, it would be the case more generic than yours.
Ag_Sharad 15-Feb-13 11:03am    
You were right, the rectangle's x and y cords were related to bitmap. But here is another easy solution i have figured out:

Bitmap bp=new Bitmap(dataGridView1.Width,dataGridView1.Height);
dataGridView1.DrawToBitmap(bp,new Rectangle(0,0,dataGridView1.Width,dataGridView1.Height));
Bitmap bp1 = bp.Clone(new Rectangle(50, 50, 100, 100), System.Drawing.Imaging.PixelFormat.DontCare);
bp1.Save("test2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

the cords of rectanlgle passed in second bitmap will crop the complete image to ur required size and u will have only cropped portion u needed.
supernorb 16-Feb-13 12:00pm    
Wow, thank you, it is another way to try (beside the one using CopyFromScreen that I mentioned in the last comment). Thank you, you should update this in the your solution to help others who have the same problem with me, I'll accept your solution first. Thanks again!

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