日期:2014-05-20  浏览次数:20900 次

分享一段C#使用指针的代码!!
这效果你懂的。

这段代码使用了不安全代码,用了指针,要运行需要更改VS配置,Google下。看代码和效果:
 
C# code
 /// <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;
        }


效果:


------解决方案--------------------
哦。。这么好的代码收藏了。。。谢谢楼主哦。。。
------解决方案--------------------
凑个热闹吧
------解决方案--------------------
探讨

引用:
哦。。这么好的代码收藏了。。。谢谢楼主哦。。。
*_* 最近研究图像效果的,顺便把这个效果共享出来,不敢独享。

------解决方案--------------------

谢谢分享
------解决方案--------------------
探讨

引用:
引用:

引用:
哦。。这么好的代码收藏了。。。谢谢楼主哦。。。
*_* 最近研究图像效果的,顺便把这个效果共享出来,不敢独享。

我来试一试
试试~~

------解决方案--------------------
如果能够自动定位需要马赛克的区域就更好了!
你们都懂的哦!
------解决方案--------------------
一般马赛克都是打到"肮脏"部位的,请不要打到人家脸上哦
------解决方案--------------------
既然你用指针,那么这几行代码,需要优化一下:
C# code

point[0] = (byte)stdB;
point[1] = (byte)stdG;
point[2] = (byte)stdR;

point[0] = (byte)pTemp[0];
point[1] = (byte)pTemp[1];
point[2] = (byte)pTemp[2];

------解决方案--------------------