c#指针进行图像操作

在写之前,这里有段源码,很适合.NET进行图像处理学习,感兴趣的下载看看

地址:http://bbs.crsite.cn/dispbbs.php?boardid=7&id=145&page=1

预览:

 

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

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


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

 

 

 1    Bitmap map  =   new  Bitmap(path);
 2     //  将图片位图区域进行锁定
 3    BitmapData mapdata  =  map.LockBits( new  Rectangle( 0 0 , 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  }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值