加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Windows窗体图片框中的图像转换

发布时间:2020-12-13 20:38:06 所属栏目:Windows 来源:网络整理
导读:我是Windows Forms的新手,在我的项目中,我需要在运行时更改图片框中的图像.我可以借助计时器来做到这一点.图片刚刚改变.是否可以在图像更改时进行一些转换,例如淡入,淡出,模糊等.如果可能的话可以让我知道如何操作.我在网上搜索但是徒劳.谢谢你提前. Varun的
我是Windows Forms的新手,在我的项目中,我需要在运行时更改图片框中的图像.我可以借助计时器来做到这一点.图片刚刚改变.是否可以在图像更改时进行一些转换,例如淡入,淡出,模糊等.如果可能的话可以让我知道如何操作.我在网上搜索但是徒劳.谢谢你提前.

Varun的

只需获取新的代码文件并粘贴下面的代码即可

类似问题的原始答案,答案取自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,this.Width,this.Height));
    else
    {
        Rectangle rc = new Rectangle(0,this.Height);
        ColorMatrix cm = new ColorMatrix();
        ImageAttributes ia = new ImageAttributes();
        cm.Matrix33 = mBlend;
        ia.SetColorMatrix(cm);
        e.Graphics.DrawImage(mImg2,rc,mImg2.Width,mImg2.Height,GraphicsUnit.Pixel,ia);
        cm.Matrix33 = 1F - mBlend;
        ia.SetColorMatrix(cm);
        e.Graphics.DrawImage(mImg1,mImg1.Width,mImg1.Height,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 Imagescf3.jpg");
        pictures[1] = new Bitmap(@"Library Imagescf4.jpg");
        pictures[2] = new Bitmap(@"Library Imagesl1.JPG");
        pictures[3] = new Bitmap(@"Library Imagesl2.JPG");
        pictures[4] = new Bitmap(@"Library Imagesl3.JPG");
        pictures[5] = new Bitmap(@"Library Imagesl4.JPG");
        pictures[6] = new Bitmap(@"Library Imagesl5.JPG");
        pictures[7] = new Bitmap(@"Library Imagesl6.JPG");
        pictures[8] = new Bitmap(@"Library Imagesl7.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;
    }

你需要修改新的Bitmap(@“yourimagePath”);调用.建立并运行.您应该看到显示的图像从第一张图像平滑变换到第二张图像,没有任何闪烁.

我希望它有助于其他……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读