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