0

Im in the process of making a snipping tool. I've got it so that my program can be used to draw a rectangle with the mouse and have that image saved. Now what I'm trying to do is have the image generated be transfered to a picture box that show's the user what they have just captured before they decide to save it or anything else.

Is there a way in which I can do this?

So far my screen capture code saves the captured image to the clipboard with the following code found in my ScreenCapture class:

 public static bool saveToClipboard = true;

     public static void CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension)
     {
         using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
         {
             using (Graphics g = Graphics.FromImage(bitmap))
             {
                 g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);

                 if (showCursor)
                 {
                     Rectangle cursorBounds = new Rectangle(curPos, curSize);
                     Cursors.Default.Draw(g, cursorBounds);
                 }
             }

             if (saveToClipboard)
             {
                 Image img = (Image)bitmap;
                 Clipboard.SetImage(img);
             }



         }
     }

Has anyone ever done something like this before? Also, is it possible to have the picture box auto resize so that the screen capture size is used and not the picture boxes?

update

Further to some of the comments below, I've been trying to save the image I store in my above class and pass it to the picture box. But nothing is displayed when I do it. The code I've been using is thus:

Held on the form1.cs:

   public void SetImage(Image img)
    {
        imagePreview.Image = img;
    }

And in the screen capture class:

       if (saveToClipboard)
        {
          Image img = (Image)bitmap;
          ControlPanel cp = new ControlPanel();
          cp.SetImage(img);
          Clipboard.SetImage(img);
        }
N0xus
  • 2,674
  • 12
  • 65
  • 126
  • 2
    well you have the `Image`, can't you just use `pictureBox.Image = img;`? – Jonesopolis Aug 21 '13 at 14:31
  • I am not shure if I get your question: Is your app on the screen and you want it to be not visible when a screenshot is taken? – Christoph Fink Aug 21 '13 at 14:39
  • Yes. I'm basically trying to remake the snipping tool that comes packaged with windows. – N0xus Aug 21 '13 at 14:42
  • Then why not just hide your window before `g.CopyFromScreen()` and show it again afterwards? – Christoph Fink Aug 21 '13 at 14:44
  • First of all why this `saveToClipboard` boolean? If it is false then why do you need to capture image? If it is always true then you don't need a boolean at all here! – Sriram Sakthivel Aug 21 '13 at 14:47
  • I am, there is more code to my project than the above class. when I hit a button, my disappears and I can drag a rectangle to get the image. Now I'm wishing to get the data withing the rectangle to be displayed in a picture box. It's this part I'm having issue with. – N0xus Aug 21 '13 at 14:48
  • It's a bool for functionality I need to add later on. For now I'm just trying to get it work with the clipboard. – N0xus Aug 21 '13 at 14:48
  • could you address my question? – Jonesopolis Aug 21 '13 at 14:56
  • Sorry. My code above is saved in another class and the picture box is on the main form. Been trying to get the img to pass over, but so far no luck. – N0xus Aug 21 '13 at 14:58
  • what about making an event that passed the image back to the form? – Jonesopolis Aug 21 '13 at 15:00
  • updated my code in the original post to show what I've been trying in regards to your idea jonesy. – N0xus Aug 21 '13 at 15:01

1 Answers1

0
ControlPanel cp = new ControlPanel();
cp.SetImage(img);

this won't work because you need to access the parent form in use, not create a new instance of it.

Look at the answer to this question on creating a simple custom event, but add an Image to the ProgressEventArgs that they create. Then on your main form, subsribe to the event and update the picturebox from there.

Community
  • 1
  • 1
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112