C# 渐变色算法 【转】

/// <summary>
        /// 渐变算法
        /// </summary>
        /// <param name="sourceImg">位图源(物理路径)</param>
        /// <param name="colorHead">起点色(HTML格式)</param>
        /// <param name="colorTail">终点色(HTML格式)</param>
        /// <param name="imgPath">保存图片路径(物理路径)</param>
        public static void LinearGradient(string sourceImg, string destinationImg, string colorHead, string colorTail)
        {
            using (Image img = Image.FromFile(sourceImg))
            {
                int width = img.Width;
                RGB colorBegin = RGB.Split(colorHead);
                RGB colorEnd = RGB.Split(colorTail);
                decimal diffR = 1m;
                decimal diffG = 1m;
                decimal diffB = 1m;
                //三原色改变步长
                diffR = Math.Round(Convert.ToDecimal(Convert.ToDecimal(colorEnd.Red - colorBegin.Red) / width), 2);
                diffG = Math.Round(Convert.ToDecimal(Convert.ToDecimal(colorEnd.Green - colorBegin.Green) / width), 2);
                diffB = Math.Round(Convert.ToDecimal(Convert.ToDecimal(colorEnd.Blue - colorBegin.Blue) / width), 2);
                RGB rgb = new RGB();
                for (int i = 0; i < width; i++)
                {
                    rgb.Red = colorBegin.Red + Convert.ToInt32(diffR * i);
                    rgb.Green = colorBegin.Green + Convert.ToInt32(diffG * i);
                    rgb.Blue = colorBegin.Blue + Convert.ToInt32(diffB * i);
                    DrawItem(img, 254, rgb, i, 1, img.Height);
                }
                img.Save(destinationImg);
            }
        }

posted @ 2013-09-18 10:56  zhh  阅读(1581)  评论(0编辑  收藏  举报