C# 光源的渐变生成算法

来源:互联网 发布:mac air wi-fi连电视上 编辑:程序博客网 时间:2024/06/05 07:30

光源的渐变生成算法



        private Bitmap CreateLightBitmap(Int32 Width,Int32 Height,Color Source,Color Destinat,Int32 s_low = -1)        {            Bitmap Bmp = new Bitmap(Width, Height);            Point Center = new Point(Width / 2, Height/2);            Int32 low = Math.Min(Center.X, Center.Y);            if (s_low > 0) low = s_low;            Int32 A = Destinat.A - Source.A;            Int32 R = Destinat.R - Source.R;            Int32 G = Destinat.G - Source.G;            Int32 B = Destinat.B - Source.B;            for (int x = 0; x < Width; x++)            {                for (int y = 0; y < Height; y++)                {                    Double _Value = Math.Sqrt(Math.Pow(Math.Abs(x - Center.X), 2) + Math.Pow(Math.Abs(y - Center.Y), 2));                    if (_Value > low) _Value = low;                    Double _bf = _Value / low * 100;                    Double _A = _bf / 100 * A;                    Double _R = _bf / 100 * R;                    Double _G = _bf / 100 * G;                    Double _B = _bf / 100 * B;                    Color nColor = Color.FromArgb(Source.A + (Int32)_A, Source.R + (Int32)_R,  Source.G + (Int32)_G, Source.B + (Int32)_B);                    Bmp.SetPixel(x, y, nColor);                }            }            return Bmp;        }

            Bitmap Bmp = CreateLightBitmap(400, 400, Color.Red, Color.Transparent);            Bmp.Save("C:\\123.png", ImageFormat.Png);            pictureBox1.Image = Bmp;



原创粉丝点击