有个部落

一晃就半个十年,思念如潮...
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[转]马赛克效果

Posted on 2011-04-11 22:24  Leo.W  阅读(234)  评论(0编辑  收藏  举报
 1 /// <summary>
 2         /// 马赛克效果
 3         ///原理:确定图像的随机位置点和确定马赛克块的大小,然后马赛克块图像覆盖随机点即可.
 4         /// </summary>
 5         /// <param name="m_Iimage"></param>
 6         /// <param name="val">分割成val*val像素的小区块</param>
 7         public Image MaSaiKe(Image m_PreImage , int val)
 8         {
 9             Bitmap MyBitmap = new Bitmap(m_PreImage);
10             if (MyBitmap.Equals(null))
11             {
12                 return null;
13             }
14             int iWidth = MyBitmap.Width;
15             int iHeight = MyBitmap.Height;
16             int stdR , stdG , stdB;
17             stdR = 0;
18             stdG = 0;
19             stdB = 0;
20             BitmapData srcData = MyBitmap.LockBits(new Rectangle(0 , 0 , iWidth , iHeight) ,
21             ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb);
22             unsafe
23             {
24                 byte* point = (byte*)srcData.Scan0.ToPointer();
25                 for (int i = 0; i < iHeight; i++)
26                 {
27                     for (int j = 0; j < iWidth; j++)
28                     {
29                         if (i % val == 0)
30                         {
31                             if (j % val == 0)
32                             {
33                                 stdR = point[2];
34                                 stdG = point[1];
35                                 stdB = point[0];
36                             }
37                             else
38                             {
39                                 point[0= (byte)stdB;
40                                 point[1= (byte)stdG;
41                                 point[2= (byte)stdR;
42                             }
43                         }
44                         else
45                         {
46                             //复制上一行  
47                             byte* pTemp = point - srcData.Stride;
48                             point[0= (byte)pTemp[0];
49                             point[1= (byte)pTemp[1];
50                             point[2= (byte)pTemp[2];
51                         }
52                         point += 3;
53                     }
54                     point += srcData.Stride - iWidth * 3;
55                 }
56                 MyBitmap.UnlockBits(srcData);
57             }
58             return MyBitmap;
59         }