简体   繁体   English

在Windows窗体中切换图像

[英]Switching Images in Windows Forms

I have 3 pictures, each one has a colored circle in it. 我有3张图片,每张图片中都有一个彩色圆圈。 The 3 pictures are red, green and yellow. 3张图片是红色,绿色和黄色。

I'm putting it in a PictureBox in a windows form. 我将其放在Windows窗体的PictureBox中。 I want to switch these images from green to yelow to red or otherwise. 我想将这些图像从绿色切换到红色或其他。

Is there anything I can make them fade to each other instead of switching them the normal way? 有什么我可以使它们彼此淡化而不是正常切换的吗?

I know this could be done using flash/j-query easily, but I was wondering how far I can achieve . 我知道可以使用flash / j-query轻松完成此操作,但是我想知道可以实现多远。

Something similar in windows forms using normal windows forms functionality. 使用常规Windows窗体功能的Windows窗体中的类似内容。

Note: I'm using .net framework 4 and windows forms. 注意:我正在使用.net framework 4和Windows窗体。

See Transition of images in Windows Forms Picture box . 请参阅Windows窗体图片框中的图像过渡 There is a solution that transitions the images using a timer on this page. 页面上有一种使用计时器转换图像的解决方案。

Code from site: 来自站点的代码:

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

Build your project. 建立您的专案。 You can now drop a BlendPanel from the top of the toolbox onto your form. 现在,您可以将BlendPanel从工具箱的顶部拖放到窗体上。 Here's a sample program that uses it: 这是一个使用它的示例程序:

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;
      }
   }
}

You'll need to modify the Bitmap.FromFile() calls. 您需要修改Bitmap.FromFile()调用。 Build and run. 生成并运行。 You should see the displayed image smoothly morph from your first image to your second image without any flickering. 您应该看到显示的图像从第一张图像平滑过渡到第二张图像,而没有任何闪烁。 Lots of ways to tweak the code, have fun. 调整代码的多种方法,尽在其中。

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

Like what @Igoris suggested, You need to use two controls overlap each other, and you should define a timer so when you want to fade in or out, start the timer and in its tick decrease the Transparent of the first control and increase it on the second one... , the problem is that the ordinary controls does not support transparent by default. 就像@Igoris建议的一样,您需要使用两个相互重叠的控件,并且应该定义一个计时器,以便当您要淡入或淡出时,启动计时器并在其刻度上减小第一个控件的“透明性”并增加它的透明度。第二个... ,问题在于默认情况下普通控件不支持透明。 so you have to inherit it and apply transparent here is a custom TransparentPictureBox that inherited from PictureBox : 因此,您必须继承它并应用透明,这是自PictureBox继承的自定义TransparentPictureBox

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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM