I want to set image on image control from scanner.
In windowsform application, I have done successfully.
My code is
private byte[] m_Frame;
private void button1_Click(object sender, EventArgs e)
{
try
{
m_hDevice = new Device();
m_hDevice.Open();
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);
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
private void OnTestGetFrame()
{
m_bCancelOperation = false;
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);
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;
}
}
How to do in WPF USING C#