在Windows窗体中切换图像

Sta*_*ker 1 c# .net-4.0 winforms

我有3张照片,每张照片都有一个彩色圆圈.这3张照片是红色,绿色和黄色.

我把它放在PictureBoxwindows窗体中.我想将这些图像从绿色切换到黄色到红色或其他.

有什么东西我可以让它们相互淡化而不是以正常方式切换它们吗?

我知道这可以很容易地使用flash/j-query完成,但我想知道我能达到多远.

使用普通窗口的Windows窗体中的类似功能.

注意:我正在使用.net框架4和Windows窗体.

SwD*_*n81 5

请参阅Windows窗体图片框中的图像转换.有一种解决方案可以使用页面上的计时器转换图像.

网站代码:

public class BlendPanel : Panel 
{
   private Image mImg1;
   private Image mImg2;
   private float mBlend;
   public BlendPanel()
   {
      SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
        ControlStyles.OptimizedDoubleBuffer, true);
   }

   public Image Image1 
   {
      get { return mImg1; }
      set { mImg1 = value; Invalidate(); }
   }

   public Image Image2 
   {
      get { return mImg2; }
      set { mImg2 = value; Invalidate(); }
   }

   public float Blend 
   {
      get { return mBlend; }
      set { mBlend = value; Invalidate(); }
   }

   protected override void OnPaint(PaintEventArgs e)
   {
      if (mImg1 == null || mImg2 == null)
      {
         e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 
            new Rectangle(0, 0, this.Width, this.Height));
      }
      else
      {
         Rectangle rc = new Rectangle(0, 0, this.Width, this.Height);
         ColorMatrix cm = new ColorMatrix();
         ImageAttributes ia = new ImageAttributes();
         cm.Matrix33 = mBlend;
         ia.SetColorMatrix(cm);
         e.Graphics.DrawImage(mImg2, rc, 0, 0, mImg2.Width, 
            mImg2.Height, GraphicsUnit.Pixel, ia);
         cm.Matrix33 = 1F - mBlend;
         ia.SetColorMatrix(cm);
         e.Graphics.DrawImage(mImg1, rc, 0, 0, mImg1.Width, 
            mImg1.Height, GraphicsUnit.Pixel, ia);
      }
      base.OnPaint(e);
   }
}
Run Code Online (Sandbox Code Playgroud)

建立你的项目.您现在可以BlendPanel从工具箱的顶部删除一个表单.这是一个使用它的示例程序:

namespace WindowsApplication1 
{
   public partial class Form1 : Form
   {
      private float mBlend;
      private int mDir = 1;
      public Form1()
      {
         InitializeComponent();
         timer1.Interval = 30;
         timer1.Tick += BlendTick;
         blendPanel1.Image1 = Bitmap.FromFile(@"c:\temp\test1.bmp");
         blendPanel1.Image2 = Bitmap.FromFile(@"c:\temp\test2.bmp");
         timer1.Enabled = true;
      }

      private void BlendTick(object sender, EventArgs e)
      {
         mBlend += mDir * 0.02F;
         if (mBlend < 0) { mBlend = 0; mDir = 1; }
         if (mBlend > 1) { mBlend = 1; mDir = -1; }
         blendPanel1.Blend = mBlend;
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

您需要修改Bitmap.FromFile()通话.建立并运行.您应该看到显示的图像从第一张图像平滑变换到第二张图像,没有任何闪烁.很多方法来调整代码,玩得开心.