StableVersion4.3/HL_FristAidPlatform_ReadCard/Form_Test.cs

100 lines
3.7 KiB
C#
Raw Permalink Normal View History

2024-03-11 09:47:34 +08:00
using System;
using System.Drawing;
using System.IO;
using System.Security.Policy;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
namespace HL_FristAidPlatform_ReadCard
{
public partial class Form_Test : System.Windows.Forms.Form
{
public Form_Test()
{
InitializeComponent();
}
private void btnRead_Click(object sender, EventArgs e)
{
IDCardData cardData = new IDCardData();
var status = IdCardReader.Read(ref cardData);
Pb_m.Image = null;
Rt_P.Text = string.Empty;
switch (status)
{
case 0:
string text = string.Format(@"
{0}
{1}
{2}
{3}
{4}
{5}
{6}
{7}
{8}",
cardData.Name,
cardData.Sex,
cardData.Nation,
cardData.Born,
cardData.Address,
cardData.IDCardNo,
cardData.GrantDept,
cardData.UserLifeBegin,
cardData.UserLifeEnd);
Rt_P.Text = text;
Bitmap bit = new Bitmap(IdCardReader.GetImgPath(cardData.IDCardNo));
var newBit = CopyBitmap(bit);
bit.Dispose();
Pb_m.Image = Image.FromHbitmap(newBit.GetHbitmap());
//BitmapFrame.Create();
//Pb_m.Image = new BitmapImage(new Uri(IdCardReader.GetImgPath(cardData.IDCardNo), UriKind.RelativeOrAbsolute));
//new BitmapImage(new Url());
break;
default:
Rt_P.Text = IdCardReader.GetStatusMsg(status);
break;
}
}
private static Bitmap CopyBitmap(Bitmap map)
{
int width = map.Width;
int height = map.Height;
Bitmap bit = new Bitmap(width, height);
using (var g = Graphics.FromImage(bit))
{
g.DrawImage(map, 0, 0, width, height);
}
return bit;
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
/// <summary>
/// 释放intptr
/// </summary>
/// <param name="hObject"></param>
public static void DisposeIntPtrObject(IntPtr hObject)
{
DeleteObject(hObject);
}
/// <summary>
/// 将bitmap转换为imagesource
/// </summary>
/// <param name="map"></param>
/// <returns></returns>
public static BitmapSource BitmapToBitmapSource(Bitmap map)
{
IntPtr hBitmap = map.GetHbitmap();
BitmapSource bitmapSource =
Imaging.CreateBitmapSourceFromHBitmap(
hBitmap, IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(hBitmap);
return bitmapSource;
}
}
}