WindowsForm PictureBox.Image为空,即使有形式显示的图像 [英] WindowsForm PictureBox.Image is null even though there's an image shown in the form

查看:2795
本文介绍了WindowsForm PictureBox.Image为空,即使有形式显示的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取该设备 - > HTTP扫描指纹:/ /www.nitgen.com/eng/product/finkey.html



我能够成功扫描指纹并保存二进制数据。我还能够显示图片框的指纹。然而,当我试图保存在图片框中显示的指纹,我得到一个错误的图片框的图像为null。



下面是我的代码。捕获指纹和保存来自PictureBox的图像

 公共Form1类:System.Windows.Forms.Form中
{
公共NBioBSPCOMLib.NBioBSP objNBioBSP;
公共NBioBSPCOMLib.IExtraction objExtraction;
私人图片框pictureExtWnd;

私人无效Form1_Load的(对象发件人,发送System.EventArgs)
{
//创建NBioBSP对象
objNBioBSP =新NBi​​oBSPCOMLib.NBioBSPClass();
objExtraction =(NBioBSPCOMLib.IExtraction)objNBioBSP.Extraction;
pictureExtWnd.Image =新位图(pictureExtWnd.Width,pictureExtWnd.Height);
}

私人无效buttonEnroll_Click(对象发件人,发送System.EventArgs)
{
//告诉NBIO不显示自己的指纹扫描窗口
objExtraction .WindowStyle = NBioBSPType.WINDOW_STYLE.INVISIBLE;

//设置指纹捕捉
的objExtraction.FPForeColor =000000的颜色;

//设置使指纹将显示
objExtraction.FPBackColor =FFFFFF的背景的颜色;

//告诉NBIO该扫描的指纹将在PictureBox的
通过给手柄控制NBIO
objExtraction.FingerWnd = pictureExtWnd.Handle.ToInt32显示//() ;

//开始扫描指纹。这也是指纹
//显示在PictureBox的。
objExtraction.Capture((INT)NBioBSPType.FIR_PURPOSE.VERIFY);

//如果在扫描指纹有没有问题,保存指纹图像
如果(objExtraction.ErrorCode == NBioBSPError.NONE)
{
字符串文件名= RandomString.GetRandomString(16,真)+.BMP;

使用(SaveFileDialog sfdlg =新SaveFileDialog())
{
sfdlg.Title =保存对话框;
sfdlg.Filter =位图图像(* .BMP)| * .BMP |所有文件(*。*)| *。*;
如果(sfdlg.ShowDialog(本)== DialogResult.OK)
{
pictureExtWnd.Image.Save(sfdlg.FileName,ImageFormat.Bmp);
MessageBox.Show(指纹保存成功。);
}
}
}
,否则
{
MessageBox.Show(指纹保存失败!);
}
}
}



我试过内部封闭

 使用(图形G =新图形)
{
objExtraction.Capture((INT)NBioBSPType

。 FIR_PURPOSE.VERIFY);
}



因为我读过,在图像上做编辑的时候,你需要使用图形。但没有明显发生,因为API没有使用图形对象实例化我



更新:
这是我落得这样做:

 使用(SaveFileDialog sfdlg =新SaveFileDialog())
{
sfdlg.Title =保存对话框
sfdlg.Filter =位图图像(* .BMP)| * .BMP |所有文件(*。*)| *。*;
如果(sfdlg.ShowDialog(本)== DialogResult.OK)
{
显卡GFX = this.pictureExtWnd.CreateGraphics();
BMP位图=新位图(this.pictureExtWnd.Width,this.pictureExtWnd.Height);
this.pictureExtWnd.DrawToBitmap(BMP,新的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(保存成功......);
}
}


解决方案

  objExtraction.FingerWnd = pictureExtWnd.Handle.ToInt32(); 

您传递的窗口句柄指纹扫描器。这就是告诉本机代码块有关,它可以绘制到一个窗口的常用方法。它通常会子类窗口过程响应WM_PAINT请求,例如,同样的想法NativeWindow.WndProc()。



但隐含的是,图像属性无用。这本机代码不知道,这是一个PictureBox控件,它具有图像属性。只知道这是为控件创建本机窗口。



不要看在API的一个选项,保存图像,这应该是可用的。如果没有,那么你在保存第一枪使用图片框DrawToBitmap()方法。其中的可以的工作,如果扫描仪实现了WM_PRINT消息处理程序。如果不工作,那么你唯一的其他备份计划是利用Graphics.CopyFromScreen()。这将总是工作,只要窗口是在前台。类似于使用你的键盘,屏幕截图上PrtSc键按钮。


I'm trying to capture the finger print scanned by this device-> http://www.nitgen.com/eng/product/finkey.html

I'm able to scan the fingerprint and save the binary data successfully. I'm also able to display the fingerprint in the picture box. However when I'm trying to save the fingerprint displayed in the picture box, I'm getting an error that the Image of the picturebox is null.

Below is my code of capturing the fingerprint and saving the image from the picturebox.

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!");
        }
    }
}

I've tried enclosing inside

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

since I've read that when doing edits on an image, you need to use graphics. but nothing is happening obviously since the api isn't using the graphic object I instantiated.

UPDATE: This is what I ended up doing:

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...");
            }
        }

解决方案

    objExtraction.FingerWnd = pictureExtWnd.Handle.ToInt32();

You passed the window handle to the fingerprint scanner. That's a common way to tell a chunk of native code about a window that it can draw to. It will typically sub-class the window procedure to respond to WM_PAINT requests, for example, same idea as NativeWindow.WndProc().

Implied however is that the Image property is useless. That native code has no idea that this is a PictureBox control and that it has an Image property. It only knows about the native window that was created for the control.

Do look in the api for an option to save the image, this ought to be available. If not then your first shot at saving it is using the picture box' DrawToBitmap() method. Which may work if the scanner implements the WM_PRINT message handler. If that doesn't work then your only other backup plan is to use Graphics.CopyFromScreen(). Which will always work, as long as the window is in the foreground. Similar to using the PrtSc button on your keyboard, a screen-shot.

这篇关于WindowsForm PictureBox.Image为空,即使有形式显示的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆