网络摄像头图像与打印图像不同

时间:2018-03-16 09:54:44

标签: c# webcam picturebox webcam-capture

我正忙着使用C#winform软件。我使用网络摄像头拍摄一张在pictureBox中显示的照片。

当我使用网络摄像头捕获图像时,它会捕获缩放的图像,而在打印时,它是一个拉伸的图像。我尝试了各种SizeMode设置。所有结果都相同

因为我不确定故障在哪里,所以我将尽可能详细地说明:

using AForge.Video;
using AForge.Video.DirectShow;

选择相机:

webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo VideoCaptureDevice in webcam)
            {
                cmbVideoList.Items.Add(VideoCaptureDevice.Name);
            }

使用相机(btn点击):

cam = new VideoCaptureDevice(webcam[cmbVideoList.SelectedIndex].MonikerString);
        cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
        cam.Start();
        if (cam.IsRunning)
        {
            btnStart.Hide();
        }
        btnStop.Show();

    }
    void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
        pboxPhoto.Image = bit;
    }

PictureBox大小:

宽度:226

身高:328

打印代码:

 PictureBox pict = (PictureBox)pboxPhoto;
        pict.SizeMode = PictureBoxSizeMode.Zoom;
        e.Graphics.DrawImage(pict.Image, 20, 416, 305, 328);

以下是软件上的图像示例: enter image description here

打印图像的样本。 enter image description here

1 个答案:

答案 0 :(得分:0)

最简单的方法是告诉PictureBox将自己绘制为Bitmap

这可以通过所有Controls完成,结果不仅包括ImageBackgroundImage,还包括Paint事件中绘制的任何内容以及任何内容嵌套Controls

这是一个简单的例子:

Size sz = pictureBox1.ClientSize;
using (Bitmap bmp = new Bitmap(sz.Width, sz.Height))
{
    pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
    // save the image..
    bmp.Save(yourfilepath, ImageFormat.Png);
    // ..or print it!
}

一些注意事项:

  • 由于在最后一次卷曲之后Bitmap获取处置,因此您可以在范围内使用它或更改为处理该范围的模式Bitmap稍后的时间。

  • 嵌套的Controls会被绘制为反向顺序,如果它们重叠会导致问题!

  • 仅包含嵌套,而非重叠 Controls。由于PictureBox不是Container(而不是Panel),因此只需将Label放在上面就可以了;相反,您需要将其嵌套在代码中,即将PictureBox设为Parent并设置合适的亲属Location ..

  • 默认情况下,Bitmap将拥有当前计算机的屏幕dpi分辨率。之后,您使用bmp.SetResolution() ..

  • 进行冷却