C# 指针操作图像

    Bitmap bmp01;

            int BmpWidth = bmp01.Width;
            int BmpHeight = bmp01.Height;
            PixelFormat BmpPixelFormat = bmp01.PixelFormat;

            byte* RBuff = (byte*)Marshal.AllocHGlobal(177600);
            byte* GBuff = (byte*)Marshal.AllocHGlobal(177600);
            byte* BBuff = (byte*)Marshal.AllocHGlobal(177600);

            BitmapData bmpData = bmp01.LockBits(new Rectangle(0, 0, BmpWidth, BmpHeight), ImageLockMode.WriteOnly, BmpPixelFormat);
            int stride = bmpData.Stride;  // 扫描线的宽度
            int offset = stride - BmpWidth*3;  // 显示宽度与扫描线宽度的间隙
            byte* pSrc = (byte*)bmpData.Scan0;  // 获取bmpData的内存起始位置

            int posScan = 0, posReal = 0;   // 分别设置两个位置指针,指向源数组和目标数组
            for (int x = 0; x < BmpHeight; x++)
            {
                //// 下面的循环节是模拟行扫描
                for (int y = 0; y < BmpWidth; y++)
                {
                    RBuff[posScan] = pSrc[posReal];
                    GBuff[posScan] = pSrc[posReal+1];
                    BBuff[posScan] = pSrc[posReal+2];

                    posScan++;
                    posReal += 3;
                }
                posReal += offset;  //行扫描结束,要将目标位置指针移过那段“间隙”
            }
             bmp01.UnlockBits(bmpData);

posted @ 2020-04-17 09:50  QuincyYi  阅读(350)  评论(0编辑  收藏  举报