简体   繁体   中英

How to save only the image that is showing from my picturebox

I am trying to figure out how to save a snapshot of a video being shown from a picturebox control. I can already save an image file, however my problem is the whole image that is 'seen' by my camera is the one that is being saved. What I would like to save is the image that is only being shown from my picturebox control, which is just a portion of what the camera is capturing. By the way I am using Aforge framework set of video libraries.

My picturebox is set at Height = 400 and Width = 400 .

Here is a sample of my code

private void Form1_Load(object sender, EventArgs e)
    {
        videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo device in videoDevices)
        {
            drvrlist.Items.Add(device.Name);
        }
        drvrlist.SelectedIndex = 1;

        videosource = new VideoCaptureDevice();

        if (videosource.IsRunning)
        {
            videosource.Stop();
        }
        else
        {
            videosource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            videosource.NewFrame += new NewFrameEventHandler(videosource_NewFrame);
            videosource.Start();
        }
    }

    private void startbtn_Click(object sender, EventArgs e)
    {
        if (videosource.IsRunning)
        {
            videosource.Stop();
        }
        else
        {
            videosource = new VideoCaptureDevice(videoDevices[drvrlist.SelectedIndex].MonikerString);
            videosource.NewFrame += new NewFrameEventHandler(videosource_NewFrame);
            videosource.Start();
        }
    }

    private void videosource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
        //throw new NotImplementedException();
    }

    private void save_btn_Click(object sender, EventArgs e)
    {
        SaveFileDialog savefilediag = new SaveFileDialog();
        savefilediag.Filter = "Portable Network Graphics|.png";
        if(savefilediag.ShowDialog()== System.Windows.Forms.DialogResult.OK)
        {
            if (pictureBox1.Image != null)
            {
                //Save First
                Bitmap varBmp = new Bitmap(pictureBox1.Image);
                Bitmap newBitmap = new Bitmap(varBmp);
                varBmp.Save(savefilediag.FileName, ImageFormat.Png);
                //Now Dispose to free the memory
                varBmp.Dispose();
                varBmp = null;
                pictureBox1.Image = null;
                pictureBox1.Invalidate();
            }
            else
            { MessageBox.Show("null exception"); }
        }
    }

You can use the Clone method to overwrite an instance of your picturebox's image with a subspace of the image.

        Bitmap varBmp = new Bitmap(pictureBox1.Image);
        varBmp = varBmp.Clone(new RectangleF(0, 0, 400, 400), varBmp.PixelFormat);

From there, you can go ahead and save it to a file.

        varBmp.Save(savefilediag.FileName, ImageFormat.Png);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM