[英]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.
顺便说一下,我正在使用Aforge框架的视频库。
My picturebox is set at Height = 400 and Width = 400 . 我的图片框设置为Height = 400和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. 您可以使用Clone方法用图片的子空间覆盖图片框图片的实例。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.