在Windows窗体中切换镜像

本文关键字:镜像 Windows 窗体 | 更新日期: 2023-09-27 18:03:46

我有3张图片,每一张都有一个彩色的圆圈。这三张图片是红色,绿色和黄色。

我把它放在windows形式的PictureBox中。我想把这些图片从绿色换成黄色再换成红色或者其他颜色

有什么我可以让他们彼此褪色,而不是切换他们的正常方式?

我知道这可以使用flash/j-query轻松完成,但我想知道我能做到多远。

在windows窗体中使用正常的windows窗体功能。

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

在Windows窗体中切换镜像

参见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);
   }
}

构建项目。您现在可以从工具箱的顶部掉落一个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;
      }
   }
}

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

我不知道这是不是一个好主意,但我会选择2个图像框,一个渐入,另一个渐出,并随着时间的推移改变alpha

就像@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;
    }
}