C# 在Windows窗体中切换图像

C# 在Windows窗体中切换图像,c#,winforms,.net-4.0,C#,Winforms,.net 4.0,我有三张照片,每一张都有一个彩色的圆圈。这三幅画是红色、绿色和黄色 我把它放在windows窗体的图片盒中。我想将这些图像从绿色切换到黄色,再切换到红色或其他颜色 有什么我可以让它们彼此淡入淡出,而不是以正常的方式切换吗 我知道使用flash/j-query可以很容易地做到这一点,但我想知道我能做到多少 windows窗体中使用普通windows窗体功能的类似内容 注意:我正在使用.net framework 4和windows窗体。我不知道这是否是个好主意,但我会选择两个图像框,一个淡入,另一

我有三张照片,每一张都有一个彩色的圆圈。这三幅画是红色、绿色和黄色

我把它放在windows窗体的
图片盒中。我想将这些图像从绿色切换到黄色,再切换到红色或其他颜色

有什么我可以让它们彼此淡入淡出,而不是以正常的方式切换吗

我知道使用flash/j-query可以很容易地做到这一点,但我想知道我能做到多少

windows窗体中使用普通windows窗体功能的类似内容


注意:我正在使用.net framework 4和windows窗体。

我不知道这是否是个好主意,但我会选择两个图像框,一个淡入,另一个淡出,并在时间流逝时更改alpha。

我不知道这是否是个好主意,但我会选择两个图像框,一个淡入,另一个淡出,并在时间流逝时更改alpha。

请参阅。有一种解决方案可以使用页面上的计时器转换图像

来自站点的代码:

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);
   }
}
构建您的项目。现在,您可以将工具箱顶部的
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;
      }
   }
}
命名空间窗口应用程序1
{
公共部分类Form1:Form
{
私人浮标;
私有int mDir=1;
公共表格1()
{
初始化组件();
时间间隔=30;
timer1.Tick+=BlendTick;
blendPanel1.Image1=Bitmap.FromFile(@“c:\temp\test1.bmp”);
blendPanel1.Image2=Bitmap.FromFile(@“c:\temp\test2.bmp”);
timer1.Enabled=true;
}
私有void BlendTick(对象发送方,事件参数e)
{
mBlend+=mDir*0.02F;
如果(mBlend<0){mBlend=0;mDir=1;}
如果(mBlend>1){mBlend=1;mDir=-1;}
blendPanel1.Blend=mBlend;
}
}
}
您需要修改
位图.FromFile()
调用。构建并运行。您应该看到显示的图像从第一个图像平滑地变形到第二个图像,并且没有任何闪烁。有很多方法可以调整代码,玩得开心。

请参阅。有一种解决方案可以使用页面上的计时器转换图像

来自站点的代码:

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);
   }
}
构建您的项目。现在,您可以将工具箱顶部的
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;
      }
   }
}
命名空间窗口应用程序1
{
公共部分类Form1:Form
{
私人浮标;
私有int mDir=1;
公共表格1()
{
初始化组件();
时间间隔=30;
timer1.Tick+=BlendTick;
blendPanel1.Image1=Bitmap.FromFile(@“c:\temp\test1.bmp”);
blendPanel1.Image2=Bitmap.FromFile(@“c:\temp\test2.bmp”);
timer1.Enabled=true;
}
私有void BlendTick(对象发送方,事件参数e)
{
mBlend+=mDir*0.02F;
如果(mBlend<0){mBlend=0;mDir=1;}
如果(mBlend>1){mBlend=1;mDir=-1;}
blendPanel1.Blend=mBlend;
}
}
}

您需要修改
位图.FromFile()
调用。构建并运行。您应该看到显示的图像从第一个图像平滑地变形到第二个图像,并且没有任何闪烁。有很多方法可以调整代码,玩得开心。

就像@Igoris建议的那样,你需要使用两个相互重叠的控件,你应该定义一个计时器,这样当你想淡入或淡出时,启动计时器并在其刻度中降低第一个控件的透明度,增加第二个控件的透明度…,问题在于,默认情况下,普通控件不支持透明。因此,您必须继承它并应用透明。这里有一个自定义的
TransparentPictureBox
,它继承自
PictureBox

public class TransparentPictureBox : System.Windows.Forms.PictureBox
{
    /// <summary>
    /// Initialize new instance of this class.
    /// </summary>
    public TransparentPictureBox()
        : base()
    {
        DoubleBuffered = true;
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.UserPaint, true);

        this.BackColor = Color.Transparent;
    }

}
公共类透明图片框:System.Windows.Forms.PictureBox
{
/// 
///初始化该类的新实例。
/// 
公共透明图片框()
:base()
{
双缓冲=真;
this.SetStyle(ControlStyles.SupportsTransparentBackColor,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
this.SetStyle(ControlStyles.UserPaint,true);
this.BackColor=Color.Transparent;
}
}

就像@Igoris建议的那样,您需要使用两个相互重叠的控件,并且您应该定义一个计时器,这样当您想要淡入或淡出时,启动计时器,并在其滴答声中降低第一个控件的透明度,增加第二个控件的透明度…,问题在于,默认情况下,普通控件不支持透明。因此,您必须继承它并应用透明。这里有一个自定义的
TransparentPictureBox
,它继承自
PictureBox

public class TransparentPictureBox : System.Windows.Forms.PictureBox
{
    /// <summary>
    /// Initialize new instance of this class.
    /// </summary>
    public TransparentPictureBox()
        : base()
    {
        DoubleBuffered = true;
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.UserPaint, true);

        this.BackColor = Color.Transparent;
    }

}
公共类透明图片框:System.Windows.Forms.PictureBox
{
/// 
///初始化该类的新实例。
/// 
公共透明图片框()
:base()
{
双缓冲=真;
this.SetStyle(ControlStyles.SupportsTransparentBackColor,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
this.SetStyle(ControlStyles.UserPaint,true);
this.BackColor=Color.Transparent;
}
}
我认为图像框(PictureBox)不支持像表单那样更改不透明度。SwDevMan81的解决方案是我喜欢的。我不认为图像框(PictureBox)支持像表单那样改变不透明度。SwDevMan81的解决方案是我会选择的。