【问题标题】:WindowsForm PictureBox.Image is null even though there's an image shown in the formWindows 窗体 PictureBox.Image 为空,即使窗体中显示了图像
【发布时间】:2012-12-20 09:18:12
【问题描述】:

我正在尝试捕获此设备扫描的指纹-> http://www.nitgen.com/eng/product/finkey.html

我能够扫描指纹并成功保存二进制数据。我还可以在图片框中显示指纹。但是,当我尝试保存图片框中显示的指纹时,我收到一个错误,即图片框的图像为空。

以下是我捕获指纹并从图片框中保存图像的代码。

public class Form1 : System.Windows.Forms.Form
{
    public NBioBSPCOMLib.NBioBSP objNBioBSP;
    public NBioBSPCOMLib.IExtraction objExtraction;
    private PictureBox pictureExtWnd;

    private void Form1_Load(object sender, System.EventArgs e)
    {
        // Create NBioBSP object
        objNBioBSP = new NBioBSPCOMLib.NBioBSPClass();
        objExtraction = (NBioBSPCOMLib.IExtraction)objNBioBSP.Extraction;
        pictureExtWnd.Image = new Bitmap(pictureExtWnd.Width, pictureExtWnd.Height);
    }

    private void buttonEnroll_Click(object sender, System.EventArgs e)
    {
        //tell NBIO to not display their fingerprint scanning window
        objExtraction.WindowStyle = NBioBSPType.WINDOW_STYLE.INVISIBLE;

        //set the color of the fingerprint captured
        objExtraction.FPForeColor = "000000";

        //set the color of the background where the fingerprint will be displayed
        objExtraction.FPBackColor = "FFFFFF";

        //tell NBIO that the scanned fingerprint will be displayed in the picturebox
        //by giving the handle control to NBIO
        objExtraction.FingerWnd = pictureExtWnd.Handle.ToInt32();

        //start scanning the fingerprint. This is also where the fingerprint
        //is displayed in the picturebox. 
        objExtraction.Capture((int)NBioBSPType.FIR_PURPOSE.VERIFY);

        //if there's no problem while scanning the fingerprint, save the fingerprint image
        if (objExtraction.ErrorCode == NBioBSPError.NONE)
        {
            string fileName = RandomString.GetRandomString(16, true) + ".bmp";

            using (SaveFileDialog sfdlg = new SaveFileDialog())
            {
                sfdlg.Title = "Save Dialog";
                sfdlg.Filter = "Bitmap Images (*.bmp)|*.bmp|All files(*.*)|*.*";
                if (sfdlg.ShowDialog(this) == DialogResult.OK)
                {
                    pictureExtWnd.Image.Save(sfdlg.FileName, ImageFormat.Bmp);
                    MessageBox.Show("FingerPrint Saved Successfully.");
                }
            }
        }
        else
        {
            MessageBox.Show("FingerPrint Saving Failed!");
        }
    }
}

我试过封闭在里面

using(Graphics g = new Graphics)
{
    objExtraction.Capture((int)NBioBSPType.FIR_PURPOSE.VERIFY);
} 

因为我已经读过,在对图像进行编辑时,您需要使用图形。但显然什么都没有发生,因为 api 没有使用我实例化的图形对象。

更新: 这就是我最终要做的:

using (SaveFileDialog sfdlg = new SaveFileDialog())
        {
            sfdlg.Title = "Save Dialog";
            sfdlg.Filter = "Bitmap Images (*.bmp)|*.bmp|All files(*.*)|*.*";
            if (sfdlg.ShowDialog(this) == DialogResult.OK)
            {
                Graphics gfx = this.pictureExtWnd.CreateGraphics();
                Bitmap bmp = new Bitmap(this.pictureExtWnd.Width, this.pictureExtWnd.Height);
                this.pictureExtWnd.DrawToBitmap(bmp, new Rectangle(0, 0, this.pictureExtWnd.Width, this.pictureExtWnd.Height));
                bmp.Save(sfdlg.FileName, ImageFormat.Bmp);

                gfx.Dispose();
                //pictureExtWnd.Image.Save(sfdlg.FileName, ImageFormat.Bmp);
                MessageBox.Show("Saved Successfully...");
            }
        }

【问题讨论】:

  • 我尝试将其封闭在 using(Graphics g = new Graphics){objExtraction.Capture((int)NBioBSPType.FIR_PURPOSE.VERIFY);} 因为我在编辑图像时读过,你需要使用图形。但显然什么都没有发生,因为 api 没有使用我实例化的图形对象
  • 请告诉我们您的问题是什么!它是否从扫描仪获取图像数据?是把图片数据放到PictureBox里吗?是否将数据写入文件?你展示了一大堆代码——据我了解你的问题——根本不相关。请只显示我们理解您的问题所绝对需要的代码。
  • 如果您阅读我帖子的第二段,您就会明白我已经能够将图像从扫描仪保存到图片框中。我在第二段中提到的问题也是,当我在做 pictureExtWnd.Image.Save(sfdlg.FileName, ImageFormat.Bmp);图片为空
  • 你试过找出原因吗?你附加了调试器吗?输入buttonEnroll_Click方法时pictureExtWnd.Image是否为空?来一个,表现出一些努力!我没有得到报酬来帮助你!

标签: c# winforms image picturebox


【解决方案1】:
    objExtraction.FingerWnd = pictureExtWnd.Handle.ToInt32();

您将窗口句柄传递给指纹扫描仪。这是一种常见的方式来告诉一大块本机代码它可以绘制到的窗口。它通常会将窗口过程子类化以响应 WM_PAINT 请求,例如,与 NativeWindow.WndProc() 的想法相同。

但暗示 Image 属性是无用的。本机代码不知道这是一个 PictureBox 控件并且它具有 Image 属性。它只知道为控件创建的本机窗口。

请在 api 中查找保存图像的选项,这应该是可用的。如果不是,那么您第一次保存它是使用图片框的 DrawToBitmap() 方法。如果扫描器实现了 WM_PRINT 消息处理程序,则 可能 起作用。如果这不起作用,那么您唯一的其他备份计划是使用 Graphics.CopyFromScreen()。只要窗口在前台,这将始终有效。类似于使用键盘上的 PrtSc 按钮,屏幕截图。

【讨论】:

  • 我已经完成了DrawToBitmap:Bitmap bmp = new Bitmap(pictureExtWnd.Width, pictureExtWnd.Height); pictureExtWnd.DrawToBitmap(bmp, new Rectangle(0, 0, pictureExtWnd.Width, pictureExtWnd.Height)); bmp.Save(sfdlg.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); 但我所保存的只是一个黑色矩形。我会试试Graphics.CopyFromScreen()。抱歉,我仍在研究如何在此评论中制作多行代码
  • 如何只捕获 CopyFromScreen 的一部分?我应该使用剪辑吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多