亲,别被吓着了---图像特效分享!(图像显示不了,重新发帖呵呵)
给大家分享一个图像处理中的特效,也可以叫做哈哈镜特效,很好玩呵呵!
原图和效果图如下:
源码demo下载地址如下:
http://download.csdn.net/detail/trent1985/4625862
主要算法代码如下:
private Bitmap srcBitmap; public static int[] GetData(Bitmap a) { try { int w = a.Width; int h = a.Height; int[] sData = new int[w * h * 3]; Bitmap dstBitmap = new Bitmap(a.Width, a.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { byte* pIn = (byte*)srcData.Scan0.ToPointer(); byte* pOut = (byte*)dstData.Scan0.ToPointer(); byte* p; int stride = srcData.Stride; int r, g, b; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { p = pIn; r = p[2]; g = p[1]; b = p[0]; sData[x * 3 + y * w * 3] = b; sData[x * 3 + 1 + y * w * 3] = g; sData[x * 3 + 2 + y * w * 3] = r; pIn += 3; pOut += 3; } pIn += srcData.Stride - w * 3; pOut += srcData.Stride - w * 3; } a.UnlockBits(srcData); dstBitmap.UnlockBits(dstData); return sData; } } catch (Exception e) { MessageBox.Show(e.Message.ToString()); return null; } } public static Bitmap Rebuildimage(int[] data, int w, int h) { try { Bitmap dstBitmap = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { byte* pOut = (byte*)dstData.Scan0.ToPointer(); int stride = dstData.Stride; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { pOut[0] = (byte)data[x * 3 + y * w * 3]; pOut[1] = (byte)data[x * 3 + 1 + y * w * 3]; pOut[2] = (byte)data[x * 3 + 2 + y * w * 3]; pOut += 3; } pOut += dstData.Stride - w * 3; } dstBitmap.UnlockBits(dstData); return dstBitmap; } } catch (Exception e) { MessageBox.Show(e.Message.ToString()); return null; } } public static int[] FunMirror(int[] srcData, double factor, int w, int h, int x, int y) { int cenX = x; int cenY = y; int newX = 0; int newY = 0; int offsetX = 0; int offsetY = 0; int radius = 0; double theta = 0; int[] tempData = (int[])srcData.Clone(); for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { int tX = i - cenX; int tY = j - cenY; theta = Math.Atan2((double)tY, (double)tX); radius = (int)Math.Sqrt((double)(tX * tX + tY * tY)); int newR = (int)(Math.Sqrt((double)radius) * 12); newX = cenX + (int)(newR * Math.Cos(theta)); newY = cenY + (int)(newR * Math.Sin(theta)); if (newX > 0 && newX < w) { offsetX = newX; } else { newX = 0; } if (newY > 0 && newY < h) { offsetY = newY; } else { newY = 0; } tempData[i * 3 + j * w * 3] = srcData[newX * 3 + newY * w * 3]; tempData[i * 3 + 1 + j * w * 3] = srcData[newX * 3 + 1 + newY * w * 3]; tempData[i * 3 + 2 + j * w * 3] = srcData[newX * 3 + 2 + newY * w * 3]; } } return tempData; } public static Bitmap DistortingMirror(Bitmap src, double degree, int x, int y) { int[] data = GetData(src); int w = src.Width; int h = src.Height; int[] dstData = FunMirror(data, degree, w, h, x, y); Bitmap dst = Rebuildimage(dstData, w, h); return dst; }