windows - Windows窗体图片框中图片的过渡

标签 windows winforms

我是 Windows 窗体的新手,在我的项目中,我需要在运行时更改图片框中的图像。我可以在计时器的帮助下做到这一点。图片只是改变了。是否可以在图像发生变化时进行一些过渡,例如淡入、淡出、模糊等。如果可能的话,请告诉我该怎么做。我在网上搜索但没有成功。提前致谢。

瓦伦

最佳答案

只需获取新的代码文件并将下面的代码粘贴到其中

类似问题的原始答案,答案来自another question

回答

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

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 从工具箱的顶部拖放到您的窗体上。这是一个使用它的示例程序:

    private float mBlend;
    private int mDir = 1;
    public int count = 0;
    public Bitmap[] pictures;

    public void myPhoto()
    {
        pictures = new Bitmap[9];
        pictures[0] = new Bitmap(@"Library Images\cf3.jpg");
        pictures[1] = new Bitmap(@"Library Images\cf4.jpg");
        pictures[2] = new Bitmap(@"Library Images\l1.JPG");
        pictures[3] = new Bitmap(@"Library Images\l2.JPG");
        pictures[4] = new Bitmap(@"Library Images\l3.JPG");
        pictures[5] = new Bitmap(@"Library Images\l4.JPG");
        pictures[6] = new Bitmap(@"Library Images\l5.JPG");
        pictures[7] = new Bitmap(@"Library Images\l6.JPG");
        pictures[8] = new Bitmap(@"Library Images\l7.JPG");

        timer1.Interval = 50; //time of transition
        timer1.Tick += BlendTick;
        try
        {
            blendPanel1.Image1 = pictures[count];
            blendPanel1.Image2 = pictures[++count];
        }
        catch
        {

        }
        timer1.Enabled = true;
    }
    private void BlendTick(object sender, EventArgs e)
    {
        mBlend += mDir * 0.02F;
        if (mBlend > 1)
        {
            mBlend = 0.0F;
            if ((count + 1) < pictures.Length)
            {
                blendPanel1.Image1 = pictures[count];
                blendPanel1.Image2 = pictures[++count];
            }
            else
            {
                blendPanel1.Image1 = pictures[count];
                blendPanel1.Image2 = pictures[0];
                count = 0;
            }
        }
        blendPanel1.Blend = mBlend;
    }

您需要修改 new Bitmap(@"yourimagePath"); 调用。构建并运行。您应该看到显示的图像从第一张图像平滑地变换到第二张图像,没有任何闪烁。

希望对其他人有所帮助

关于windows - Windows窗体图片框中图片的过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3270919/

相关文章:

WPF 相当于 Windows 窗体中的 'Control.CheckForIllegalCrossThreadCalls'

windows - 在 PowerShell 中将位置设置为新安装的磁盘镜像失败

windows - 列出非域系统上所有用户的上次 Windows 密码更改

.net - 更流畅地渲染 winforms 控件

winforms - 发布Windows窗体应用程序如何更改安装路径

c# - Cursor.Current 与 this.Cursor

c# - 如何更改 winform DataGridview 标题的颜色?

windows - 在Windows 10上链接两个Docker容器

c++ - Windows 性能调整的资源建议(实时)

windows - 如何确定是否使用(最好)仅批处理安装了 Windows 服务?