热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

C#实现的水纹特效

发布时间:2010年03月06日点击数: 佚名
  1. using System; 
  2. using System.Drawing; 
  3. using System.Drawing.Imaging; 
  4. namespace WaterWave 
  5.     public sealed unsafe class WaterWave : IDisposable 
  6.     { 
  7.         private Bitmap _orgImage = null
  8.         private Bitmap _newImage = null
  9.         private BitmapData _orgData = null
  10.         private byte* _pOrgBase; 
  11.         private int _width; 
  12.         private int[,] _buf1, _buf2; 
  13.         private int _rippleCount = int.MaxValue; 
  14.         public WaterWave(Bitmap bitmap) 
  15.         { 
  16.             _orgImage = (Bitmap)bitmap.Clone(); 
  17.             _newImage = (Bitmap)bitmap.Clone(); 
  18.             Width = bitmap.Width; 
  19.             Height = bitmap.Height; 
  20.             _orgData = _orgImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
  21.             _pOrgBase = (byte*)_orgData.Scan0.ToPointer(); 
  22.             _width = Width * 3; 
  23.             if(_width % 4 != 0) 
  24.                 _width = 4 * (_width / 4 + 1); 
  25.             _buf1 = new int[Width, Height]; 
  26.             _buf2 = new int[Width, Height]; 
  27.         } 
  28.         /// <summary> 
  29.         /// 图片宽度 
  30.         /// </summary> 
  31.         public int Width { getprivate set; } 
  32.         /// <summary> 
  33.         /// 图片高度 
  34.         /// </summary> 
  35.         public int Height { getprivate set; } 
  36.         /// <summary> 
  37.         /// 扩散 
  38.         /// </summary> 
  39.         private void RippleSpread() 
  40.         { 
  41.             for(int i = 1;i < Width - 1;i++) 
  42.                 for(int j = 1;j < Height - 1;j++) 
  43.                 { 
  44.                     _buf2[i, j] = ((_buf1[i - 1, j] + _buf1[i + 1, j] + _buf1[i, j - 1] + _buf1[i, j + 1]) >> 1) - _buf2[i, j]; 
  45.                     _buf2[i, j] -= _buf2[i, j] >> 5; 
  46.                 } 
  47.             int[,] t = _buf1; 
  48.             _buf1 = _buf2; 
  49.             _buf2 = t; 
  50.         } 
  51.         /// <summary> 
  52.         /// 重绘 
  53.         /// </summary> 
  54.         private void RenderRipple() 
  55.         { 
  56.             _newImage = new Bitmap(Width, Height); 
  57.             BitmapData newData = _newImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
  58.             byte* pBase = (byte*)newData.Scan0.ToPointer(); 
  59.             int dx, dy; 
  60.             for(int i = 1;i < Width - 1;i++) 
  61.                 for(int j = 1;j < Height - 1;j++) 
  62.                 { 
  63.                     dx = _buf1[i, j - 1] - _buf1[i, j + 1]; 
  64.                     dy = _buf1[i - 1, j] - _buf1[i + 1, j]; 
  65.                     if(i + dx < 0 || i + dx >= Width || j + dy < 0 || j + dy >= Height) 
  66.                         continue
  67.                     //fmap.SetColor(i, j, FastOriginal.GetColor(i + dx, j + dy)); 
  68.                     *(pBase + j * _width + i * 3) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3); 
  69.                     *(pBase + j * _width + i * 3 + 1) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3 + 1); 
  70.                     *(pBase + j * _width + i * 3 + 2) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3 + 2); 
  71.                 } 
  72.             _newImage.UnlockBits(newData); 
  73.         } 
  74.         /// <summary> 
  75.         /// 增加波源 
  76.         /// </summary> 
  77.         /// <param name="point"></param> 
  78.         /// <param name="deep"></param> 
  79.         public void DropStone(Point point) 
  80.         { 
  81.             _buf1[point.X, point.Y] -= 100; 
  82.             _rippleCount = 90; 
  83.         } 
  84.         /// <summary> 
  85.         /// 获取当前一帧图像 
  86.         /// </summary> 
  87.         /// <returns></returns> 
  88.         public Bitmap GetFrame() 
  89.         { 
  90.             if(_rippleCount > 0) 
  91.             { 
  92.                 RippleSpread(); 
  93.                 RenderRipple(); 
  94.                 _rippleCount--; 
  95.             } 
  96.             return _newImage; 
  97.         } 
  98.         #region IDisposable Members 
  99.         public void Dispose() 
  100.         { 
  101.             if(_orgData != null
  102.                 _orgImage.UnlockBits(_orgData); 
  103.             _orgData = null
  104.         } 
  105.         #endregion 
  106.     } 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作