Esempio n. 1
0
        public EImageBW8 ConvertBitmapToEImageBW8(Bitmap bmp)
        {
            EImageBW8 EBW8Image1 = new EImageBW8(bmp.Width, bmp.Height); // EImageBW8 instance

            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                //锁定位图
                System.Drawing.Imaging.BitmapData bmpdata_src = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                //获取首行地址
                IntPtr pScan0 = bmpdata_src.Scan0;
                unsafe
                {
                    try
                    {
                        for (int Height = 0; Height < bmpdata_src.Height; Height++)
                        {
                            byte *pSrc = (byte *)pScan0;
                            pSrc += bmpdata_src.Stride * Height;
                            byte *pDest = (byte *)EBW8Image1.GetImagePtr(0, Height);
                            for (int Width = 0; Width < bmpdata_src.Width; Width++)
                            {
                                pDest[0] = (byte)(pSrc[0] * 0.299 + pSrc[1] * 0.587 + pSrc[2] * 0.114);
                                pSrc    += 3;
                                pDest++;
                            }
                        }
                    }
                    catch { }
                }
                bmp.UnlockBits(bmpdata_src);
            }
            catch { }
            return(EBW8Image1);
        }