diff --git a/test/scannerlib/huagao/c#/WindowsFormsApp1/Form1.cs b/test/scannerlib/huagao/c#/WindowsFormsApp1/Form1.cs index 0c3ffee3..1b9cd070 100644 --- a/test/scannerlib/huagao/c#/WindowsFormsApp1/Form1.cs +++ b/test/scannerlib/huagao/c#/WindowsFormsApp1/Form1.cs @@ -43,26 +43,57 @@ namespace WindowsFormsApp1 imageInfo.origin = 0; HGScannerLib.HGLib_GetImageInfo(image, ref imageInfo); - Bitmap curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); - - Rectangle rect = new Rectangle(0, 0, (int)imageInfo.width, (int)imageInfo.height); - - System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb); - unsafe + Bitmap curBitmap = null; + if (imageInfo.type == 1) // BINARY { - byte* dest = (byte*)(bmpData.Scan0); - byte* src = (byte*)imageData; - for (int i = 0; i < (int)imageInfo.height; i++) + curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.widthStep, + System.Drawing.Imaging.PixelFormat.Format1bppIndexed, imageData); + + System.Drawing.Imaging.ColorPalette palette = curBitmap.Palette; + palette.Entries[0] = Color.FromArgb(0, 0, 0); + palette.Entries[1] = Color.FromArgb(255, 255, 255); + curBitmap.Palette = palette; + } + else if (imageInfo.type == 2) // GRAY + { + curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.widthStep, + System.Drawing.Imaging.PixelFormat.Format8bppIndexed, imageData); + + System.Drawing.Imaging.ColorPalette palette = curBitmap.Palette; + for (int i = 0; i < palette.Entries.Length; i++) { - for (int j = 0; j < (int)imageInfo.width; j++) + palette.Entries[i] = Color.FromArgb(i, i, i); + } + curBitmap.Palette = palette; + } + else if (imageInfo.type == 3) // BGR + { + curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.widthStep, + System.Drawing.Imaging.PixelFormat.Format24bppRgb, imageData); + } + else if (imageInfo.type == 4) // RGB + { + curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); + + Rectangle rect = new Rectangle(0, 0, (int)imageInfo.width, (int)imageInfo.height); + + System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb); + unsafe + { + byte* dest = (byte*)(bmpData.Scan0); + byte* src = (byte*)imageData; + for (int i = 0; i < (int)imageInfo.height; i++) { - dest[i * bmpData.Stride + j * 3 + 0] = src[i * imageInfo.widthStep + j * 3 + 2]; - dest[i * bmpData.Stride + j * 3 + 1] = src[i * imageInfo.widthStep + j * 3 + 1]; - dest[i * bmpData.Stride + j * 3 + 2] = src[i * imageInfo.widthStep + j * 3 + 0]; + for (int j = 0; j < (int)imageInfo.width; j++) + { + dest[i * bmpData.Stride + j * 3 + 0] = src[i * imageInfo.widthStep + j * 3 + 2]; + dest[i * bmpData.Stride + j * 3 + 1] = src[i * imageInfo.widthStep + j * 3 + 1]; + dest[i * bmpData.Stride + j * 3 + 2] = src[i * imageInfo.widthStep + j * 3 + 0]; + } } } + curBitmap.UnlockBits(bmpData); } - curBitmap.UnlockBits(bmpData); curBitmap.Save("3.jpg");