原文:
Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效
/// <summary> /// Image merge process. /// </summary> /// <param name="bacImage">The background image.</param> /// <param name="dstImage">The source image.</param> /// <param name="k">One parameter, from 0 to 1.</param> /// <returns></returns> public static WriteableBitmap ImageMerge(WriteableBitmap bacImage, WriteableBitmap dstImage, double k) { if (bacImage != null && dstImage != null) { int w = dstImage.PixelWidth; int h = dstImage.PixelHeight; int sw = bacImage.PixelWidth; int sh = bacImage.PixelHeight; WriteableBitmap srcImage = new WriteableBitmap(w, h); byte[] dstValue = dstImage.PixelBuffer.ToArray(); byte[] bacValue = bacImage.PixelBuffer.ToArray(); byte[] tempValue = new byte[dstValue.Length]; int r = 0, g = 0, b = 0, R = 0, G = 0, B = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int tx = x % sw; int ty = y % sh; b = bacValue[tx * 4 + ty * w * 4]; g = bacValue[tx * 4 + 1 + ty * w * 4]; r = bacValue[tx * 4 + 2 + ty * w * 4]; B = dstValue[x * 4 + y * w * 4]; G = dstValue[x * 4 + 1 + y * w * 4]; R = dstValue[x * 4 + 2 + y * w * 4]; double xr = 0.0, xb = 0.0, xg = 0.0; xr = ((double)r - ((double)R - (double)k * 255.0)) / (2.0 * 255.0 * k); xg = ((double)g - ((double)G - (double)k * 255.0)) / (2.0 * 255.0 * k); xb = ((double)b - ((double)B - (double)k * 255.0)) / (2.0 * 255.0 * k); tempValue[x * 4 + y * w * 4] = (byte)(255.0 * (1.0 - 3.0 * xb * xb + 2.0 * xb * xb * xb)); tempValue[x * 4 + 1 + y * w * 4] = (byte)(255.0 * (1.0 - 3.0 * xg * xg + 2.0 * xg * xg * xg)); tempValue[x * 4 + 2 + y * w * 4] = (byte)(255.0 * (1.0 - 3.0 * xr * xr + 2.0 * xr * xr * xr)); } } Stream sTemp = srcImage.PixelBuffer.AsStream(); sTemp.Seek(0, SeekOrigin.Begin); sTemp.Write(tempValue, 0, w * 4 * h); return srcImage; } else { return null; } }
最后,分享一个专业的图像处理网站(微像素),里面有很多源代码下载: