【问题标题】:Webcam image differ from printed image网络摄像头图像与打印图像不同
【发布时间】:2018-03-20 03:00:32
【问题描述】:

我正忙于一个 C# winform 软件。我使用网络摄像头拍摄显示在图片框中的图片。

当我使用网络摄像头拍摄图像时,它会拍摄一张放大的图像,而在打印时,它是一张拉伸的图像。我尝试了各种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;
    }

图片框大小:

宽度: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

【问题讨论】:

  • PBox 图像始终是原始图像。您可以使用 DrawToBitmap 创建缩放版本或使用两个 Rectangle 进行绘制以自己进行任何拉伸..
  • 会尝试的。谢谢。我是 C# 新手,是否在“打印代码”中使用 DrawToBitMap?
  • 是或者只要图像准备好了..
  • @TaW 我正在与 DrawToBitMap 作斗争。你能帮我提供示例代码吗?
  • using (Bitmap bmp = new Bitmap(pboxPhoto.Width, pboxPhoto.Height)) { pboxPhoto.DrawToBitmap(bmp, new Rectangle( Point.Empty, bmp.Size)); //bmp.Save("D:\\patternDraw_.png", ImageFormat.Png); // or print it! } - 请注意,如果 PBox 有边框,您应该将大小调整为 pboxPhoto.ClientSze.Width 等。

标签: c# webcam picturebox webcam-capture


【解决方案1】:

最简单的方法是告诉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!
}

几点说明:

    1234563 @ 在稍后的某个时间点。
  • 嵌套的Controls 将按reverse 顺序绘制,如果它们重叠会导致问题!

  • 仅包含嵌套,不包含叠加 Controls。由于PictureBox 不是Container(与例如Panel 相对),因此不能简单地将Label 放在上面;相反,您需要将其嵌套在代码中,即将 PictureBox 设为 Parent 并设置合适的相对 Location..

  • 默认情况下,Bitmap 将具有当前机器的屏幕dpi 分辨率。之后你用bmp.SetResolution()..

  • 冷掉它

【讨论】:

    猜你喜欢
    • 2020-07-06
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多