Win8 Metro(C#)數字圖像處理--2.73一種背景圖像融合特效

原文: 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;
            }
        }

最後,分享一個專業的圖像處理網站(微像素),裏面有不少源代碼下載:
相關文章
相關標籤/搜索