Exemplo n.º 1
0
        private static void TransferPixelsGreyscale(Bitmap bitmap_dest,
                                                    TwainDotNet.TwainNative.ImageInfo imageInfo, TwainDotNet.TwainNative.ImageMemXfer memxfer_src)
        {
            BitmapInfoHeaderIndexedColor bitmapInfo = new BitmapInfoHeaderIndexedColor();

            bitmapInfo.Width  = (int)memxfer_src.Columns;
            bitmapInfo.Height = -(int)memxfer_src.Rows;
            // bitmapInfo.Size = sizeof(BitmapInfoHeader);   // requires unsafe
            bitmapInfo.Size      = 40; // the size of the initial header, not including color table
            bitmapInfo.Planes    = 1;
            bitmapInfo.SizeImage = 0;
            bitmapInfo.BitCount  = imageInfo.BitsPerPixel;

            if (imageInfo.Planar == (short)TwainNative.TwainBool.True)
            {
                throw new TwainException("Planar format invalid for greyscale data.");
            }

            if (imageInfo.BitsPerPixel == 8)
            {
                BitmapInfoHeaderIndexedColor.setupGreyscaleIndices(ref bitmapInfo);
                bitmapInfo.ClrUsed = 256;
            }
            else if (imageInfo.BitsPerPixel == 1)
            {
                BitmapInfoHeaderIndexedColor.setupBWIndices(ref bitmapInfo);
                bitmapInfo.ClrUsed = 2;
            }
            else
            {
                throw new TwainException("TransferPixelsGreyscale() only supports 8 bits per pixel");
            }


            using (Graphics graphics = Graphics.FromImage(bitmap_dest))
            {
                IntPtr hdc = graphics.GetHdc();
                try
                {
                    Gdi32Native.SetDIBitsToDevice(
                        hdc,
                        (int)memxfer_src.XOffset,
                        (int)memxfer_src.YOffset,
                        (int)memxfer_src.Columns,
                        (int)memxfer_src.Rows,
                        0, 0, 0,
                        (int)memxfer_src.Rows,
                        memxfer_src.Memory.TheMem,
                        bitmapInfo,
                        0);
                }
                finally
                {
                    graphics.ReleaseHdc(hdc);
                }
            }
        }
Exemplo n.º 2
0
        public static void setupGreyscaleIndices(ref BitmapInfoHeaderIndexedColor hdr)
        {
            // we use reflection to populate the greyscale table to avoid typing a giant hardcoded table
            Object ohdr  = hdr;
            Type   otype = hdr.GetType();

            for (int i = 0; i < 256; i++)
            {
                var field = otype.GetField(String.Format("bmiColor_{0}", i), BindingFlags.Instance | BindingFlags.NonPublic);
                field.SetValue(ohdr, new RGBQuad((byte)i, (byte)i, (byte)i));
            }
            hdr = (BitmapInfoHeaderIndexedColor)ohdr;
        }
Exemplo n.º 3
0
 public static void setupBWIndices(ref BitmapInfoHeaderIndexedColor hdr)
 {
     hdr.bmiColor_0 = new RGBQuad(0, 0, 0);        // black
     hdr.bmiColor_1 = new RGBQuad(255, 255, 255);  // white
 }
Exemplo n.º 4
0
 public static extern int SetDIBitsToDevice(IntPtr hdc, int xdst, int ydst, int width, int height,
                                            int xsrc, int ysrc, int start, int lines, IntPtr bitsptr, [In] BitmapInfoHeaderIndexedColor bmiptr, int colorUse);