欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

gif 渐变动画 .net

程序员文章站 2022-06-08 08:53:15
...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace giftest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
               
/// <summary>
/// [颜色:RGB转成16进制]
/// </summary>
/// <param name="R">红 int</param>
/// <param name="G">绿 int</param>
/// <param name="B">蓝 int</param>
/// <returns></returns>
public static string colorRGBtoHx16(int R, int G, int B)
{
return System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(R, G, B));
}


private static string ToHexColor(Color color)
{
string R = Convert.ToString(color.R, 16);
if (R == "0")
R = "00";
string G = Convert.ToString(color.G, 16);
if (G == "0")
G = "00";
string B = Convert.ToString(color.B, 16);
if (B == "0")
B = "00";
string HexColor = "#" + R + G + B;
return HexColor;
}

       public static Color retrieveRGBComponent(int color)
         {  
              var r = color >> 16;  
              var g = (color >> 8) & 0xff;  
              var b = color & 0xff;

             
              return Color.FromArgb(r,g,b);
          }  
             
           /** 
            * 红色,绿色和蓝色三色组合 
            */
         public static Color generateFromRGBComponent(Color rgb)
           {  
               if( rgb == null ||
                   rgb.R < 0 || rgb.R > 255 ||
                   rgb.G < 0 || rgb.G > 255 ||
                   rgb.B < 0 || rgb.B > 255)  
                   return Color.Black;
               return rgb;
           }  
             
           /** 
            * color1是浅色,color2是深色,实现渐变 
            * steps是指在多大的区域中渐变, 
            */
         public static List<Color> generateTransitionalColor(Color color1, Color color2, int steps)  
           {
               List<Color> colors = new List<Color>();
               if( steps < 3 )  
                {   
                   return null;
                }
               Color color1RGB = color1;
               Color color2RGB = color2;  
                 
               
               colors.Add(color1);  
               steps = steps - 2;  
                 
               var redDiff = color2RGB.R - color1RGB.R;  
               var greenDiff = color2RGB.G - color1RGB.G;  
               var blueDiff = color2RGB.B - color1RGB.B;  
               for( var i = 1; i < steps - 1; i++)  
               {
                   var tempRGB = Color.FromArgb(
                       color1RGB.R + redDiff * i / steps,  
                       color1RGB.G + greenDiff * i / steps,  
                       color1RGB.B + blueDiff * i / steps  
                  );

                  colors.Add(tempRGB);  
               }
               colors.Add(color2);  
                 
               return colors;  
           }  

        //生成动画代码
        private void button1_Click(object sender, EventArgs e)
        {
            AnimatedGifEncoder gif = new AnimatedGifEncoder();
            gif.Start("hello.gif");
            gif.SetRepeat(0);
            gif.SetDelay(400);
            gif.SetFrameRate(24);
            
            //生成渐变颜色数据
            List<Color> colors = generateTransitionalColor(Color.Blue, Color.Red, 30);
            List<Color> colors2 = generateTransitionalColor(Color.GreenYellow, Color.Red, 30);
            int index=0;
            foreach (Color color in colors)
            {
                Bitmap bit = new Bitmap(200, 100);//2个100*100的渐变图片
                Graphics g=Graphics.FromImage(bit);
             
                g.FillRectangle(new SolidBrush(color), 0, 0, 100, 100);
                g.FillRectangle(new SolidBrush(colors2[index]), 100, 0, 100, 100);
                index++;
                gif.AddFrame(bit);
                g.Dispose();
                bit.Dispose();
            }
            gif.Finish();

            
        }
    }
}

 
gif 渐变动画 .net
            
    
    博客分类: .net  
 

 

  • gif 渐变动画 .net
            
    
    博客分类: .net  
  • 大小: 34.3 KB