Windows Develop Bookmark and Share   
 index > Windows Forms General > CopyFromScreen PictureBox
 

CopyFromScreen PictureBox

Hi,

I'm encountering a problem using the PictureBox / Control.BackgroundImage. I'm trying to capture parts / entire screen using CopyFromScreen method. After capturing, I will print the Image onto the PictureBox.Image / Form's backgroundImage. I have succesfully captured the image and tested and by saving on a file. Now, my problem is that everytime I capture the Screen and print it out onto the PictureBox/ Form's Background Image, it prints out a big X, indicating the bitmap doesn't exist (afaik). I've tried using try catch method to catch necessary exceptions but nothing is being catched.

What possible solution on the problem?

Here's the code:


    public partial class Form1 : Form
    {
        public Form1 ()
        {
            InitializeComponent ();
        }

        private void button1_Click (object sender, EventArgs e)
        {
            using (Bitmap capturedImage = new Bitmap (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppRgb))
            {
                // Create a graphics object from the bitmap

                using (Graphics gfx = Graphics.FromImage (capturedImage))
                {
                    // Take the screenshot from the upper left corner to the right bottom corner                    

                    gfx.CopyFromScreen (Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

                    //capturedImage.Save ("sample.png", ImageFormat.Png);

                    //gfx.DrawImage (capturedImage, new Rectangle (0, 0, 100, 100));
//pictureBox1.Image = capturedImage;
} pictureBox1.Image = capturedImage; } } }

Thanks.
  • Edited bynoObzoR Sunday, July 19, 2009 5:28 AM
  • Edited bynoObzoR Sunday, July 19, 2009 5:29 AM
  •  
noObzoR  Sunday, July 19, 2009 5:26 AM
You dispose the image immediately after you assign it to the pictureBox.
JohnWein  Sunday, July 19, 2009 7:15 AM
You dispose the image immediately after you assign it to the pictureBox.
JohnWein  Sunday, July 19, 2009 7:15 AM
Oh yeah how noob I am haha. Now, I'm trying to zoom in what I captured before setting up as the form's background. How can I do that?
noObzoR  Sunday, July 19, 2009 7:31 AM
Start a new thread for your new question.

Hans Passant.
nobugz  Sunday, July 19, 2009 1:07 PM

You can use google to search for other answers

Custom Search

More Threads

• How to set the transparent color for an Animated GIF?
• ActiveDocumentHost - Menus
• How to force form2 to run in front of main form using with modal
• Open a childform in MDI Parent from anothet childform
• Looking for best approach for MSMoney Look-A-Like [VB]
• Enabling AxMSFlexGrid to multilingual
• UserControl DefaultValue
• Access DB of web site using Windows forms...
• Button inside a textbox?
• About Box Show