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); /// /// 释放intptr /// /// public static void DisposeIntPtrObject(IntPtr hObject) { DeleteObject(hObject); } /// /// 将bitmap转换为imagesource /// /// /// 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; } } }