最近在网上寻找此类代码,发现并没有C#实现的。原理都很简单,自己都能找到。文章地址:http://blog.csdn.net/aimeast/archive/2009/11/01/4755578.aspx
下载地址:http://download.csdn.net/source/1787773效果图:
核心代码:using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace WaterWave
{
public sealed unsafe class WaterWave : IDisposable
{
private Bitmap _orgImage = null;
private Bitmap _newImage = null;
private BitmapData _orgData = null;
private byte* _pOrgBase;
private int _width;
private int[,] _buf1, _buf2;
private int _rippleCount = int.MaxValue;
public WaterWave(Bitmap bitmap)
{
_orgImage = (Bitmap)bitmap.Clone();
_newImage = (Bitmap)bitmap.Clone();
Width = bitmap.Width;
Height = bitmap.Height;
_orgData = _orgImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
_pOrgBase = (byte*)_orgData.Scan0.ToPointer();
_width = Width * 3;
if(_width % 4 != 0)
_width = 4 * (_width / 4 + 1);
_buf1 = new int[Width, Height];
_buf2 = new int[Width, Height];
}
/// <summary>
/// 图片宽度
/// </summary>
public int Width { get; private set; }
/// <summary>
/// 图片高度
/// </summary>
public int Height { get; private set; }
/// <summary>
/// 扩散
/// </summary>
private void RippleSpread()
{
for(int i = 1;i < Width - 1;i++)
for(int j = 1;j < Height - 1;j++)
{
_buf2[i, j] = ((_buf1[i - 1, j] + _buf1[i + 1, j] + _buf1[i, j - 1] + _buf1[i, j + 1]) >> 1) - _buf2[i, j];
_buf2[i, j] -= _buf2[i, j] >> 5;
}
int[,] t = _buf1;
_buf1 = _buf2;
_buf2 = t;
}
/// <summary>
/// 重绘
/// </summary>
private void RenderRipple()
{
_newImage = new Bitmap(Width, Height);
BitmapData newData = _newImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* pBase = (byte*)newData.Scan0.ToPointer();
int dx, dy;
for(int i = 1;i < Width - 1;i++)
for(int j = 1;j < Height - 1;j++)
{
dx = _buf1[i, j - 1] - _buf1[i, j + 1];
dy = _buf1[i - 1, j] - _buf1[i + 1, j];
if(i + dx < 0 || i + dx >= Width || j + dy < 0 || j + dy >= Height)
continue;
//fmap.SetColor(i, j, FastOriginal.GetColor(i + dx, j + dy));
*(pBase + j * _width + i * 3) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3);
*(pBase + j * _width + i * 3 + 1) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3 + 1);
*(pBase + j * _width + i * 3 + 2) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3 + 2);
}
_newImage.UnlockBits(newData);
}
/// <summary>
/// 增加波源
/// </summary>
/// <param name="point"></param>
/// <param name="deep"></param>
public void DropStone(Point point)
{
_buf1[point.X, point.Y] -= 100;
_rippleCount = 90;
}
/// <summary>
/// 获取当前一帧图像
/// </summary>
/// <returns></returns>
public Bitmap GetFrame()
{
if(_rippleCount > 0)
{
RippleSpread();
RenderRipple();
_rippleCount--;
}
return _newImage;
}
#region IDisposable Members
public void Dispose()
{
if(_orgData != null)
_orgImage.UnlockBits(_orgData);
_orgData = null;
}
#endregion
}
}
下载地址:http://download.csdn.net/source/1787773效果图:
核心代码:using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace WaterWave
{
public sealed unsafe class WaterWave : IDisposable
{
private Bitmap _orgImage = null;
private Bitmap _newImage = null;
private BitmapData _orgData = null;
private byte* _pOrgBase;
private int _width;
private int[,] _buf1, _buf2;
private int _rippleCount = int.MaxValue;
public WaterWave(Bitmap bitmap)
{
_orgImage = (Bitmap)bitmap.Clone();
_newImage = (Bitmap)bitmap.Clone();
Width = bitmap.Width;
Height = bitmap.Height;
_orgData = _orgImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
_pOrgBase = (byte*)_orgData.Scan0.ToPointer();
_width = Width * 3;
if(_width % 4 != 0)
_width = 4 * (_width / 4 + 1);
_buf1 = new int[Width, Height];
_buf2 = new int[Width, Height];
}
/// <summary>
/// 图片宽度
/// </summary>
public int Width { get; private set; }
/// <summary>
/// 图片高度
/// </summary>
public int Height { get; private set; }
/// <summary>
/// 扩散
/// </summary>
private void RippleSpread()
{
for(int i = 1;i < Width - 1;i++)
for(int j = 1;j < Height - 1;j++)
{
_buf2[i, j] = ((_buf1[i - 1, j] + _buf1[i + 1, j] + _buf1[i, j - 1] + _buf1[i, j + 1]) >> 1) - _buf2[i, j];
_buf2[i, j] -= _buf2[i, j] >> 5;
}
int[,] t = _buf1;
_buf1 = _buf2;
_buf2 = t;
}
/// <summary>
/// 重绘
/// </summary>
private void RenderRipple()
{
_newImage = new Bitmap(Width, Height);
BitmapData newData = _newImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* pBase = (byte*)newData.Scan0.ToPointer();
int dx, dy;
for(int i = 1;i < Width - 1;i++)
for(int j = 1;j < Height - 1;j++)
{
dx = _buf1[i, j - 1] - _buf1[i, j + 1];
dy = _buf1[i - 1, j] - _buf1[i + 1, j];
if(i + dx < 0 || i + dx >= Width || j + dy < 0 || j + dy >= Height)
continue;
//fmap.SetColor(i, j, FastOriginal.GetColor(i + dx, j + dy));
*(pBase + j * _width + i * 3) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3);
*(pBase + j * _width + i * 3 + 1) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3 + 1);
*(pBase + j * _width + i * 3 + 2) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3 + 2);
}
_newImage.UnlockBits(newData);
}
/// <summary>
/// 增加波源
/// </summary>
/// <param name="point"></param>
/// <param name="deep"></param>
public void DropStone(Point point)
{
_buf1[point.X, point.Y] -= 100;
_rippleCount = 90;
}
/// <summary>
/// 获取当前一帧图像
/// </summary>
/// <returns></returns>
public Bitmap GetFrame()
{
if(_rippleCount > 0)
{
RippleSpread();
RenderRipple();
_rippleCount--;
}
return _newImage;
}
#region IDisposable Members
public void Dispose()
{
if(_orgData != null)
_orgImage.UnlockBits(_orgData);
_orgData = null;
}
#endregion
}
}
解决方案 »
- 问个很奇怪的数字筛选问题
- !!C#调用C版DLL-IntPtr如何用?!!!
- winform界面切换 二.
- 关于WPF的Style
- 哪位大侠知道CPU 序号的编码规则?是否完全唯一?有没有固定位?
- 如何使任务栏里的长条闪烁起来?或换个颜色也行。就向msn messenger在对话窗口没有输入焦点,接收到信息时的样子。
- C# Winform 多线程界面假死
- 网上投了上百封简历居然没有一个回应。真心求教 投简历诀窍
- 如何知道正在连接的sqlserver数据库是在本机上还是在其他电脑上?
- 怎样将数据库查询得到的表返回以供其它的函数使用?在线等
- 熟悉C++的大侠来下,转成C#的
- 在大型系统中怎样处理并发及锁的问题(SQL Server中)
for(int j = 1;j < Height - 1;j++)
{
dx = _buf1[i, j - 1] - _buf1[i, j + 1];
dy = _buf1[i - 1, j] - _buf1[i + 1, j];
if(i + dx < 0 || i + dx >= Width || j + dy < 0 || j + dy >= Height)
continue;
//fmap.SetColor(i, j, FastOriginal.GetColor(i + dx, j + dy));
*(pBase + j * _width + i * 3) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3);
*(pBase + j * _width + i * 3 + 1) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3 + 1);
*(pBase + j * _width + i * 3 + 2) = *(_pOrgBase + (j + dy) * _width + (i + dx) * 3 + 2);
}
这代码,效率太低了。
http://download.csdn.net/source/1787773