C# – Imaging – XYMosaic (XY馬賽克)
來源:互聯網
上載者:User
寫此段代碼的原因是為了證明坎子的程式的可最佳化性,隨寫此代碼~~~當然,上班時間偷閑的匆匆之作,My Code依然可最佳化~~~比如用填充的方法填充一個單一顏色的位元影像,然後排布這個位元影像,來做馬賽克效果~~~
誰有心思,寫一個吧~~
^_^
坎子的代碼:http://www.cnblogs.com/iceshark/archive/2004/12/12/75362.html
更新了一下,去掉了BUG一處~~
XYMosaic( Bitmap ObjectBitmap , uint Stride )#region XYMosaic( Bitmap ObjectBitmap , uint Stride )
public static void XYMosaic(Bitmap ObjectBitmap, uint Stride)
{
if(ObjectBitmap.Equals(null))
{
new Exception("The Bitmap is null.");
}
try
{
int stride = (int)Stride;
int centerW = (stride - 1) / 2;;
int centerH = centerW;
int newH = ObjectBitmap.Height / stride + 1;
int newW = ObjectBitmap.Width / stride + 1;
int tmpH, tmpW;
int lW = ObjectBitmap.Width % stride;
int lH = ObjectBitmap.Height % stride;
int ti, tj;
Color c;
for(int w = 0; w < newW; w++)
{
tmpW = w * stride;
if(w.Equals(newW - 1))
{
ti = lW;
}
else
{
ti = stride;
}
centerW = (ti - 1) / 2;
for(int h = 0; h < newH; h++)
{
tmpH = h * stride;
if(h.Equals(newH - 1))
{
tj = lH;
}
else
{
tj = stride;
}
centerH = (tj - 1) / 2;
c = ObjectBitmap.GetPixel(tmpW + centerW, tmpH + centerH);
for(int i = 0; i < ti; i++)
{
for(int j = 0; j < tj; j++)
{
ObjectBitmap.SetPixel(tmpW + i, tmpH + j, c);
}
}
}
}
}
catch(Exception e)
{
new Exception("Mosaic Error", e);
}
}
#endregion