By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,022 Members | 1,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,022 IT Pros & Developers. It's quick & easy.

Copy part of Windows form into Clipboard.

P: n/a
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

Oct 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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/sc...select=1597128)
would be a good alternative.

Oct 22 '06 #2

P: n/a
"Ajith Menon" <ko********@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
>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
Oct 22 '06 #3

P: n/a
My Screen Shot app (with c# source) may give you some ideas:
http://channel9.msdn.com/ShowPost.as...=191076#191076

--
William Stacey [C# MVP]

"Ajith Menon" <ko********@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
|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
|
Oct 23 '06 #4

P: n/a
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

Oct 25 '06 #5

P: n/a
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

Oct 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.