C# 如何仅保存从我的picturebox中显示的图像
我正在试图找出如何保存从picturebox控件显示的视频快照。我已经可以保存一个图像文件,但是我的问题是,我的相机“看到”的整个图像就是正在保存的图像。我想保存的是仅从picturebox控件显示的图像,这只是相机正在捕获的图像的一部分。顺便说一下,我正在使用一套大型视频库框架 我的picturebox设置为高度=400和宽度=400 这是我的代码示例C# 如何仅保存从我的picturebox中显示的图像,c#,image,crop,picturebox,video-capture,C#,Image,Crop,Picturebox,Video Capture,我正在试图找出如何保存从picturebox控件显示的视频快照。我已经可以保存一个图像文件,但是我的问题是,我的相机“看到”的整个图像就是正在保存的图像。我想保存的是仅从picturebox控件显示的图像,这只是相机正在捕获的图像的一部分。顺便说一下,我正在使用一套大型视频库框架 我的picturebox设置为高度=400和宽度=400 这是我的代码示例 private void Form1_Load(object sender, EventArgs e) { video
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"); }
}
}
您可以使用克隆方法用图像的子空间覆盖picturebox图像的实例
Bitmap varBmp = new Bitmap(pictureBox1.Image);
varBmp = varBmp.Clone(new RectangleF(0, 0, 400, 400), varBmp.PixelFormat);
从那里,您可以继续并将其保存到文件中
varBmp.Save(savefilediag.FileName, ImageFormat.Png);
工作得很有魅力。多谢各位