Copy part of Windows form into Clipboard.

  • Thread starter Thread starter Ajith Menon
  • Start date Start date
A

Ajith Menon

I want to copy a part of the windows form into clipboard as .bmp file.
The usecase is the user drags using the mouse and creates a rectangular
selection any where on the form. This part has to be copied into
clipboard.

Can anybody help me in implementing this using C#?

Thanks,
Ajith
 
This will be moderately complicated, but there are fortunately multiple
ways to go about it.

You could use SendKeys, then Print Screen, and then get the image
stream and using an image editing library for C# to crop the image.

You could also use some parts of the source code of this program:
http://blogs.geekdojo.net/brian/articles/Cropper.aspx

You could also use a library specifically intended for taking
screenshots. There was one I saw before but I couldn't find it, this
(http://web6.codeproject.com/tools/screen_snaper.asp?df=100&forumid=13697&exp=0&select=1597128)
would be a good alternative.
 
Ajith Menon said:
I want to copy a part of the windows form into clipboard as .bmp file.
The usecase is the user drags using the mouse and creates a rectangular
selection any where on the form. This part has to be copied into
clipboard.

Can anybody help me in implementing this using C#?

Not having done this myself, I can't say for sure whether this is a valid
way to use a C# Form. However, the first approach I'd try is to call the
Form's OnPaint method directly, providing in the event args a Graphics
object set to draw into whatever destination you desire. In this case, it
would be a Graphics for a bitmap, which you can then save to the clipboard
(or anywhere else you like).

As the simplest case, saving the entire Form as a bitmap, you'd create a
bitmap object the same size as the Form itself, and draw to that. To save
only a subset of the form, I can think of a couple of alternatives. One
would be to do the exact same thing, and then copy the portion of the drawn
bitmap to a new one that is the desired size. Another would be to start
with the final bitmap size, adjusting the transform for the Graphics object
for that bitmap so that the rendering for the Form is shifted to account for
the displaced origin of the destination bitmap.

I can't say that I have a lot of faith in the idea of using some external
screenshot functionality. For one, it creates an unnecessary dependency on
components outside your control, and for another I don't see any way to
guarantee that when the screenshot of your Form is taken, it is completely
unobscured.

Pete
 
My Screen Shot app (with c# source) may give you some ideas:
http://channel9.msdn.com/ShowPost.aspx?PostID=191076#191076

--
William Stacey [C# MVP]

|I want to copy a part of the windows form into clipboard as .bmp file.
| The usecase is the user drags using the mouse and creates a rectangular
| selection any where on the form. This part has to be copied into
| clipboard.
|
| Can anybody help me in implementing this using C#?
|
| Thanks,
| Ajith
|
 
Thank you very much for all your help.

The problem which i am facing is that there are many other controls
over the form that completely fills up the form.
The rectangle can be created on the plain form, but if there are
controls over it then the rectangle will not be visible to the user.


Somthing of this sort can be implemented using unmanaged code (see the
code below). I need the alternative for this in C# using framework 1.1.

hdc = CreateDC ("DISPLAY", NULL, NULL, NULL);
hPenNew = CreatePen (PS_DASH, 0, RGB(0,0,0));

hPenOld = static_cast<HPEN>(SelectObject(hdc, hPenNew));
SetROP2 (hdc, R2_XORPEN); // toggle pixels
SelectObject (hdc, GetStockObject (NULL_BRUSH)); // no fill

/* erase previous rectangle */
Rectangle (hdc, ptAnchor.x, ptAnchor.y, ptEnd.x, ptEnd.y);

/* draw new rectangle */
ClientToScreen (hwnd, &_ptEnd);
ptEnd = _ptEnd;
Rectangle (hdc, ptAnchor.x, ptAnchor.y, ptEnd.x, ptEnd.y);


Thanks in advance.

Regards,
Ajith
 
Thank you very much for all your help.

The problem which i am facing is that there are many other controls
over the form that completely fills up the form.
The rectangle can be created on the plain form, but if there are
controls over it then the rectangle will not be visible to the user.


Somthing of this sort can be implemented using unmanaged code (see the
code below). I need the alternative for this in C# using framework 1.1.

hdc = CreateDC ("DISPLAY", NULL, NULL, NULL);
hPenNew = CreatePen (PS_DASH, 0, RGB(0,0,0));

hPenOld = static_cast<HPEN>(SelectObject(hdc, hPenNew));
SetROP2 (hdc, R2_XORPEN); // toggle pixels
SelectObject (hdc, GetStockObject (NULL_BRUSH)); // no fill

/* erase previous rectangle */
Rectangle (hdc, ptAnchor.x, ptAnchor.y, ptEnd.x, ptEnd.y);

/* draw new rectangle */
ClientToScreen (hwnd, &_ptEnd);
ptEnd = _ptEnd;
Rectangle (hdc, ptAnchor.x, ptAnchor.y, ptEnd.x, ptEnd.y);


Thanks in advance.

Regards,
Ajith
 
Back
Top