Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to set image on image control from scanner.
In windowsform application, I have done successfully.
My code is
C#
private byte[] m_Frame;
private void button1_Click(object sender, EventArgs e)
        {     
      
           try
            {
                m_hDevice = new Device();
                m_hDevice.Open();

                // gets devce parameters
                VersionInfo version = m_hDevice.VersionInformation;
               

                DeviceInfo dinfo = m_hDevice.Information;
                if (dinfo.DeviceCompatibility == 0)
                {
                    m_lblCompatibility.Text = "USB 1.1 device";
                }
                else if (dinfo.DeviceCompatibility == 1)
                {
                    m_lblCompatibility.Text = "USB 2.0 device";
                }
                else
                {

                    string g = dinfo.ToString();
                    m_lblCompatibility.Text = "Unknown";
                }
                m_btnClose.Enabled = true;
                OnTestGetFrame();



            }
            catch (ScanAPIException ex)
            {
                if (m_hDevice != null)
                {
                    m_hDevice.Dispose();
                    m_hDevice = null;
                }
                ShowError(ex);
            }
        }


    private void OnTestGetFrame()
        {
            m_bCancelOperation = false;
            MessageBox.Show(this, "Put finger to the scanner", this.Text, this.OnUserBreak);
            GetFrame(); 
            MessageDlg.HideMessageDlg();
            if (m_Frame != null && m_Frame.Length != 0)
            {
                Size size = m_hDevice.ImageSize;
                Graphics gr = m_picture.CreateGraphics();
                Bitmap hBitmap = CreateBitmap(gr.GetHdc(), size, m_Frame);
              hBitmap.Save(@"C:\Users\ABC\Desktop\Enrollment\abc.bmp");
                hBitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
                m_picture.Image = hBitmap;
               
            }
        }
private void GetFrame()
        {
            int LastErrorCode = 0;
            bool bContinue = true;
            do
            {
                while (!m_hDevice.IsFingerPresent && !m_bCancelOperation)
                {
                    if (LastErrorCode != m_hDevice.LastErrorCode)
                    {
                        LastErrorCode = m_hDevice.LastErrorCode;
                        switch (LastErrorCode)
                        {
                            case 0:
                                break;
                            case FTR_ERROR_EMPTY_FRAME:
                                MessageBox.Show("Put finger to the scanner");
                                break;
                            case FTR_ERROR_MOVABLE_FINGER:
                                MessageBox.Show("There is no stable fingerprint image on the device.");
                                break;
                            case FTR_ERROR_NO_FRAME:
                                MessageBox.Show("Fake finger was detected");
                                break;
                            case FTR_ERROR_HARDWARE_INCOMPATIBLE:
                            case FTR_ERROR_FIRMWARE_INCOMPATIBLE:
                                MessageBox.Show("The device does not support the requested feature");
                                break;
                            default:
                                String msg = String.Format("Unknown error #{0}", LastErrorCode);
                                MessageBox.Show("msg");
                                break;
                        }
                    }
                    Thread.Sleep(0);
                }
                if (!m_bCancelOperation)
                {
                    try
                    {
                        m_Frame = m_hDevice.GetFrame();
                        bContinue = false;
                    }
                    catch (ScanAPIException)
                    {
                        LastErrorCode = m_hDevice.LastErrorCode;
                        switch (LastErrorCode)
                        {
                            case 0:
                                break;
                            case FTR_ERROR_EMPTY_FRAME:
                                MessageBox.Show("Put finger to the scanner");
                                break;
                            case FTR_ERROR_MOVABLE_FINGER:
                                MessageBox.Show("There is no stable fingerprint image on the device.");
                                break;
                            case FTR_ERROR_NO_FRAME:
                                MessageBox.Show("Fake finger was detected");
                                break;
                            case FTR_ERROR_HARDWARE_INCOMPATIBLE:
                            case FTR_ERROR_FIRMWARE_INCOMPATIBLE:
                                MessageBox.Show("The device does not support the requested feature");
                                break;
                            default:
                                String msg = String.Format("Unknown error #{0}", LastErrorCode);
                                MessageBox.Show("msg");
                                break;
                        }
                    }
                }
                else
                {
                    break;
                }
            } while (bContinue);
        }
private Bitmap CreateBitmap(IntPtr hDC, System.Drawing.Size bmpSize, byte[] data)
        {
            System.IO.MemoryStream mem = new System.IO.MemoryStream();
            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(mem);
            //BITMAPINFO bmpInfo = new BITMAPINFO();
            BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER();
            bmiHeader.biSize = 40;
            bmiHeader.biWidth = bmpSize.Width;
            bmiHeader.biHeight = bmpSize.Height;
            bmiHeader.biPlanes = 1;
            bmiHeader.biBitCount = 8;
            bmiHeader.biCompression = BI_RGB;
            bw.Write(bmiHeader.biSize);
            bw.Write(bmiHeader.biWidth);
            bw.Write(bmiHeader.biHeight);
            bw.Write(bmiHeader.biPlanes);
            bw.Write(bmiHeader.biBitCount);
            bw.Write(bmiHeader.biCompression);
            bw.Write(bmiHeader.biSizeImage);
            bw.Write(bmiHeader.biXPelsPerMeter);
            bw.Write(bmiHeader.biYPelsPerMeter);
            bw.Write(bmiHeader.biClrUsed);
            bw.Write(bmiHeader.biClrImportant);

            for (int i = 0; i < 256; i++)
            {
                bw.Write((byte)i);
                bw.Write((byte)i);
                bw.Write((byte)i);
                bw.Write((byte)0);
            }

            IntPtr hBitmap;
            if (data != null)
            {
                hBitmap = CreateDIBitmap(hDC, ref bmiHeader, CBM_INIT, data, mem.ToArray(), DIB_RGB_COLORS);
            }
            else
            {
                hBitmap = CreateDIBitmap(hDC, ref bmiHeader, 0, null, mem.ToArray(), DIB_RGB_COLORS);
            }
            return Bitmap.FromHbitmap(hBitmap);
        }

m_picture is a picture control.

But in wpf m_picture is a Image control.
Code using c# in wpf application is
C#
private void OnTestGetFrame()
       {
           m_bCancelOperation = false;
           //MessageDlg.ShowMessageDlg(this, "Put finger to the scanner", this.Text, this.OnUserBreak);
           GetFrame();

           if (m_Frame != null && m_Frame.Length != 0)
           {
LiveFinger.Source = byteArrayToImage(m_Frame);
           }
       }
private BitmapImage byteArrayToImage(byte[] byteArrayIn)
       {
           try
           {
               MemoryStream stream = new MemoryStream();
               stream.Write(byteArrayIn, 0, byteArrayIn.Length);
               stream.Position = 0;
               System.Drawing.Image img = System.Drawing.Image.FromStream(stream); //Exception here parameter is invalid
               BitmapImage returnImage = new BitmapImage();
               returnImage.BeginInit();
               MemoryStream ms = new MemoryStream();
               img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
               ms.Seek(0, SeekOrigin.Begin);
               returnImage.StreamSource = ms;
               returnImage.EndInit();

               return returnImage;
           }
           catch (Exception ex)
           {
               return null;
               //throw ex;
           }

       }

How to do in WPF USING C#
Posted
Updated 10-Mar-13 21:46pm
v2

1 solution

XML
Use window.form control in wpf
in window tag
Collapse | Copy Code

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

add a window.form.picturebox
Collapse | Copy Code

<windowsformshost>
 <wf:picturebox x:name="m_picture" xmlns:x="#unknown" xmlns:wf="#unknown"> </wf:picturebox></windowsformshost>


C#
Size size = m_hDevice.ImageSize;
                Graphics gr = m_picture.CreateGraphics();
                Bitmap hBitmap = CreateBitmap(gr.GetHdc(), size, m_Frame);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900