C# 中图片框的图像之间的转换[重复]

2024-03-28

可能的重复:
Windows 窗体图片框中的图像转换 https://stackoverflow.com/questions/3270919/transition-of-images-in-windows-forms-picture-box

我使用下面的代码每 5 秒更改图片框中的图像,但更改图像时看起来不太好:我想要图像之间的过渡效果。

使用的代码

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Bitmap[] 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");

        Random random = new Random();
        while (true)
        {
            int attempt = random.Next(0, pictures.Length);
            pictureBox1.Image = pictures[attempt];
            System.Threading.Thread.Sleep(5000);
        }
    }

示例代码非常感谢,提前致谢...


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

类似问题的原始答案,答案取自另一个问题 https://stackoverflow.com/questions/5519956/drawimage-with-opacity/5520096#5520096

Answer

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");来电。构建并运行。您应该看到显示的图像从第一张图像平滑地过渡到第二张图像,没有任何闪烁。

我希望它对其他人有帮助...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 中图片框的图像之间的转换[重复] 的相关文章

随机推荐

  • 如何通过 ASP.NET Core/Razor 单击按钮来运行在 Razor 页面上编写的 C#?

    我想在单击页面上的按钮时执行我直接在 razor 页面上编写的 C 方法 我发现 如果我引用了页面上的按钮 它会在第一次加载时执行该方法 但当我实际单击该按钮时 它不会再次执行 这是代码 Razor 页面 C 参考 functions in
  • Go 1.13 RSS持续增加,疑似清理问题

    我们的一项 Go 服务中的 RSS 不断增加 我们怀疑这是由于 scavenger 没有正确地将内存返回给操作系统 或者操作系统由于使用 MADV FREE 而没有收回内存 通过 pprof 检查 未检测到内存泄漏 我们尝试使用以下简单的
  • Facebook API - 无法获取页面个人资料图像

    我有一个页面无法从中获取公共个人资料图像 为什么会这样 为什么会发生 有问题的页面是 警告 脱衣舞娘的 NSFW 风格图像 真的很抱歉 我只能找到示例 无论如何都不需要查看该页面 https www facebook com pages T
  • Java Applet 不缓存

    我部署的 Java 小程序有问题 该小程序拒绝缓存在 jvm 的 粘性 缓存 或浏览器 中 由于某种原因 每次用户加载该小程序所在的页面时 jvm 都会从服务器重新下载 jar 文件 这会导致长时间延迟 包含该小程序的网页是通过互联网访问的
  • 如何在 Windows 机器上构建或安装 freeglut

    我下载了freeglut http freeglut sourceforge net 我正在阅读安装指示 http freeglut sourceforge net docs install php 我提取了所有文件 我看到一个名为conf
  • 无服务器框架Python lambda直接返回JSON

    我试图找出如何使用无服务器框架直接以 JSON 形式返回响应 这是 AWS 上具有 Lambda 代理集成的功能 全部默认设置 目标是从 python lambda 函数中 HTTP 响应客户端直接获取的是 JSON 对象 而不是 JSON
  • 在javascript中访问数组中的数组

    我收到一个 JSON 回复 如下所示 order id 12 customer user user status Pending date added 02 09 2012 total 500 00 action text View hre
  • 如何使用回调来保证顺序执行?

    我正在尝试围绕回调进行思考 但我不明白回调是如何进行的保证将执行一条语句之后 就时间而言 另一个声明需要一个unknown多少时间 我不在乎承诺 等待 异步等但只是我想学习的简单回调 例如下面的例子 我的方法将在未知时间事件发生之前执行回调
  • 从网页中的 servlet 读取 Quicktime 电影?

    我有一个 Servlet 它通过从服务器读取文件来构造对媒体文件请求的响应 File uploadFile new File C TEMP movie mov FileInputStream in new FileInputStream u
  • 如何创建使用 ng-model 的角度日期选择器指令

    我为 jQuery UI 日期选择器创建了一个角度指令 问题是 当选择日期时 指令不会更新输入的 ng model 知道为什么吗 http jsbin com ufoqan 1 edit http jsbin com ufoqan 1 ed
  • 最小化/小型化可可 NSWindow 没有标题栏

    我被困住了 显然 因为我在这里发布了一个问题 我为我的 OS X cocoa 应用程序构建了自己的自定义窗口控件 关闭按钮效果很好 没问题 当我禁用标题栏时 最小化按钮根本不起作用 因此 当标题栏像上图一样打开并且我点击此方法时 最小化效果
  • 查询谷歌电子表格的特定工作表

    我正在尝试使用谷歌电子表格作为临时数据库 我已按照以下教程中的说明进行操作 一切正常 http www alatechsource org blog 2012 05 using the google spreadsheets data ap
  • PHP:如何自动加载接口和摘要

    我有这个自动装载机要自动加载的类classes最初 但现在我想自动加载interfaces and abstracts以及 所以我做了以下改变answer https stackoverflow com questions 7924782
  • 在 PHP echo 中嵌入 javascript

    echo td manuf td 上面的这个行得通吗 我正在从 mysql 数据库中提取结果来编辑内容 但需要 jQuery 功能来编辑它 因此嵌入了 javascript 变量 EDIT 抱歉 缺乏上下文 它与我在这里提出的另一个问题有关
  • 无法使用 ggsurvplot 从列表中使用 survfit 对象绘制 kaplan-meier 曲线

    我正在尝试使用 survminer 包中的 ggsurvplot 绘制 Kaplan Meyer 曲线 当我传递保存在列表中的 survfit 对象时 我无法绘制它 让我以肺部数据集为例 一切正常如下 library survival li
  • Vulkan 管道顶部/底部和 ALL_COMMANDS

    作为很多 初学者 我认为使用 TOP OF PIPELINE 作为 dst 和 BOTTOM OF PIPELINE 作为 src 意味着两者的 ALL COMMANDS Here https github com KhronosGroup
  • Grails 2.0 中带有新 where 查询的参数

    在 Grails 2 0 中定义 where 查询时是否可以使用参数 例如 def query Book where id it Book sub query find 5 我尝试运行该代码 但它在调用 find 时抛出 MissingMe
  • 创建一个包含比原始元素更多元素的 ReactiveUI 派生集合

    是否可以创建一个 ReactiveUI 派生集合 其中包含比原始集合更多的元素 我已经看到有一种方法可以过滤集合并选择单个属性 但我正在寻找相当于可枚举的 SelectMany 操作 为了说明这一点 想象一下尝试获取代表每个陷入交通拥堵的乘
  • 避免 printf() 中的尾随零

    我一直在发现 printf 系列函数的格式说明符 我想要的是能够打印小数点后最大给定位数的双精度 或浮点数 如果我使用 printf 1 3f 359 01335 printf 1 3f 359 00999 I get 359 013 35
  • C# 中图片框的图像之间的转换[重复]

    这个问题在这里已经有答案了 可能的重复 Windows 窗体图片框中的图像转换 https stackoverflow com questions 3270919 transition of images in windows forms