Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 14-Feb-13 21:27pm
supernorb2.6K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 rectangle passed in second bitmap will crop the complete image to ur required size and u will have only cropped portion u needed.
  Permalink  
v2
Comments
supernorb at 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 at 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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100