分享一段C#使用指针的代码!! 马赛克效果

来自:http://bbs.csdn.net/topics/360146578

这效果你懂的。
这段代码使用了不安全代码,用了指针,要运行需要更改VS配置,Google下。看代码和效果:

 /// <summary>
        /// 马赛克效果
        ///原理:确定图像的随机位置点和确定马赛克块的大小,然后马赛克块图像覆盖随机点即可.
        /// </summary>
        /// <param name="m_Iimage"></param>
        /// <param name="val">分割成val*val像素的小区块</param>
        public Image MaSaiKe(Image m_PreImage , int val)
        {
            Bitmap MyBitmap = new Bitmap(m_PreImage);
            if (MyBitmap.Equals(null))
            {
                return null;
            }
            int iWidth = MyBitmap.Width;
            int iHeight = MyBitmap.Height;
            int stdR , stdG , stdB;
            stdR = 0;
            stdG = 0;
            stdB = 0;
            BitmapData srcData = MyBitmap.LockBits(new Rectangle(0 , 0 , iWidth , iHeight) ,
            ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb);
            unsafe
            {
                byte* point = (byte*)srcData.Scan0.ToPointer();
                for (int i = 0; i < iHeight; i++)
                {
                    for (int j = 0; j < iWidth; j++)
                    {
                        if (i % val == 0)
                        {
                            if (j % val == 0)
                            {
                                stdR = point[2];
                                stdG = point[1];
                                stdB = point[0];
                            }
                            else
                            {
                                point[0] = (byte)stdB;
                                point[1] = (byte)stdG;
                                point[2] = (byte)stdR;
                            }
                        }
                        else
                        {
                            //复制上一行  
                            byte* pTemp = point - srcData.Stride;
                            point[0] = (byte)pTemp[0];
                            point[1] = (byte)pTemp[1];
                            point[2] = (byte)pTemp[2];
                        }
                        point += 3;
                    }
                    point += srcData.Stride - iWidth * 3;
                }
                MyBitmap.UnlockBits(srcData);
            }
            return MyBitmap;
        }
效果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值