c#指针进行图像操作

提及.NET的指针操作,很多人并不是很了解,甚至还不知道有这么个东东
由于C#的指针操作属于unsafe操作,所以很多人对unsafe使用起来都很谨慎

其实所谓不安全代码,其实是不受控于CLR控制下的托管执行,相当于CLR领导下的部分区域自治,
当然CLR也不会去关心不安全代码的内存分配和回收


费话少说,先体验一下适当的指针操作带来的性能提升。
平时我们通过GDI+操作稍大点图片象素时,都会有一种濒临崩溃的感觉,我们转下用指针操作

 

 

 1   Bitmap map = new Bitmap(path);
 2   // 将图片位图区域进行锁定
 3   BitmapData mapdata = map.LockBits(new Rectangle(00, map.Width, map.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
 4   unsafe
 5   {
 6       byte* pixpoint;
 7       int newpoint;
 8 
 9       for (int i = 0; i < map.Width; i++)
10       {
11           for (int j = 0; j < map.Height; j++)
12           {
13               // 位图结构存在一个字节对齐问题。      
14               pixpoint = (byte*)mapdata.Scan0; +* 3 + j * mapdata.Stride;
15               newpoint = (*pixpoint * 11 + *(pixpoint + 1* 59 + *(pixpoint + 2* 30/ 100;
16               *(pixpoint++= (byte)newpoint;
17               *(pixpoint++= (byte)newpoint;
18               *(pixpoint++= (byte)newpoint;
19 
20               //  非指针操作
21               //  Color pxcl=map.GetPixel(i, j);
22               // map.SetPixel(i, j, (pxcl.R * 11 + pxcl.G * 59 + pxcl * 30) / 100);
23           }
24       }
25   }
26   // 将位图内存区域进行解锁
27   map.UnlockBits(mapdata);
28   map.Dispose();

比较GDI+使用的GetPixel/SetPixel,让你立马感到这个世界多么美妙


再来一个中值滤波的操作

   

 1 /// <summary>
 2 /// 中值滤波
 3 /// </summary>
 4 /// <param name="dgGrayValue"></param>
 5 public void MedianFilter(int dgGrayValue)
 6 {
 7 //   100M 双线空间 + 50M数据库  28元/年
 8 //   1G 双线空间    + 200M数据库  100元/年
 9 //                                            QQ:70975363
10     byte s;
11     byte[] p = new byte[9];
12     int i, j;
13     int x, y;
14     int Stride;
15     unsafe
16     {
17         byte* point = (byte*)this.ImageData.Scan0;
18         Stride = this.ImageData.Stride;
19         point = point + 3 + this.ImageData.Stride;
20         for (i = 0; i < this.ImageData.Height - 1; i++)
21         {
22             for (j = 0; j < this.ImageData.Width - 1; j++)
23             {
24                 p[0= *(point - Stride - 1);
25                 p[1= *(point - Stride + 2);
26                 p[2= *(point - Stride + 5);
27                 p[3= *(point - 1);
28                 p[4= *(point + 2);
29                 p[5= *(point + 5);
30                 p[6= *(point + Stride - 1);
31                 p[7= *(point + Stride + 2);
32                 p[8= *(point + Stride + 5);
33                 for (x = 0; x < 5; x++)
34                 {
35                     for (y = x + 1; y < 9; y++)
36                     {
37                         if (p[x] > p[y])
38                         {
39                             s = p[x];
40                             p[x] = p[y];
41                             p[y] = s;
42                         }
43                     }
44                 }
45                  *point = p[4];
46                 *(point - 1= p[4];
47                 *(point - 2= p[4];
48                   point += 3;
49 
50             }
51             point += Stride - this.ImageData.Width * 3;
52         }
53     }   
54 }
55 
posted @ 2010-05-28 08:55  逆时针  阅读(803)  评论(0编辑  收藏  举报