如何仅保存从我的图片框中显示的图像

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

我想弄清楚如何保存从图片框控件显示的视频的快照。我已经可以保存一个图像文件,但是我的问题是我的相机 'seen' 的整个图像正在被保存。我想保存的是仅从我的图片框控件中显示的图像,这只是相机捕获的一部分。顺便说一下,我使用的是 Aforge 框架视频库集。

我的图片框设置为 高度 = 400宽度 = 400

这是我的代码示例

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"); }
        }
    }

您可以使用克隆方法用图像的子空间覆盖图片框图像的实例。

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

从那里,您可以继续将其保存到文件中。

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