private static BitmapImage ConvertToPngBitmapImage(UInt32 Width, UInt32 Height, byte[] imageRawBytes) { //int bitsPerPixel = ((int)PixelFormat.Format8bppIndexed >> 8) & 0xFF; //int validBitsPerLine = width * bitsPerPixel; //int stride = ((validBitsPerLine + 31) / 32) * 4; GCHandle handle = GCHandle.Alloc(imageRawBytes, GCHandleType.Pinned); Bitmap src = new Bitmap((int)Width, (int)Height, (int)Width, PixelFormat.Format8bppIndexed, handle.AddrOfPinnedObject()); ColorPalette _palette = src.Palette; Color[] _entries = _palette.Entries; for (int i = 0; i < 256; i++) { Color b = new Color(); b = Color.FromArgb((byte)i, (byte)i, (byte)i); _entries[i] = b; } src.Palette = _palette; MemoryStream ms = new MemoryStream(); ((System.Drawing.Bitmap)src).Save(ms, System.Drawing.Imaging.ImageFormat.Png); BitmapImage image = new BitmapImage(); image.BeginInit(); ms.Seek(0, SeekOrigin.Begin); image.StreamSource = ms; image.EndInit(); handle.Free(); return image; } private static void SaveImage(BitmapImage image, string localFilePath) { var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create((BitmapImage)image)); using (var filestream = new FileStream(localFilePath, FileMode.Create)) { encoder.Save(filestream); } } IntPtr pImage = IntPtr.Zero; UInt32 width = 0; UInt32 height = 0; BS2ErrorCode sdkResult = (BS2ErrorCode)API.BS2_GetLastFingerprintImage(sdkContext, deviceId, out pImage, out width, out height); byte[] imageRawBytes = new byte[width * height]; Marshal.Copy(pImage, imageRawBytes, 0, imageRawBytes.Length); API.BS2_ReleaseObject(pImage); BitmapImage bitmapImage = ConvertToPngBitmapImage(width, height , imageRawBytes); string filepath = String.Format(".\\{0}_{1}_{2}.png", DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"), deviceID, seq); Console.WriteLine("Save " + filepath); SaveImage(bitmapImage, filepath);