随笔 - 3461, 文章 - 0, 评论 - 739, 阅读 - 1200万
  管理
复制代码
public partial class Form1 : Form
{
    private Timer fadeTimer;
    private int fadeValue = 0;
    private bool fadeIn = true;
 
    public Form1()
    {
        InitializeComponent();
        fadeTimer = new Timer();
        fadeTimer.Interval = 100; // 设置定时器的时间间隔,单位为毫秒
        fadeTimer.Tick += new EventHandler(OnFadeTimerTick);
    }
 
    private void OnFadeTimerTick(object sender, EventArgs e)
    {
        if (fadeIn)
        {
            fadeValue += 10;
            if (fadeValue >= 255)
            {
                fadeIn = false;
                fadeValue = 255;
            }
        }
        else
        {
            fadeValue -= 10;
            if (fadeValue <= 0)
            {
                fadeIn = true;
                fadeValue = 0;
            }
        }
 
        pictureBox1.Image = SetImageOpacity(pictureBox1.Image, fadeValue);
    }
 
    private Image SetImageOpacity(Image image, int opacity)
    {
        // 创建一个和原图片一样大小的Bitmap,并设置其透明度
        Bitmap bmp = new Bitmap(image.Width, image.Height);
 
        using (Graphics g = Graphics.FromImage(bmp))
        {
            ColorMatrix matrix = new ColorMatrix();
 
            matrix.Matrix33 = opacity / 255.0f;
            var attributes = new ImageAttributes();
            attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
 
            g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
                0, 0, image.Width, image.Height,
                GraphicsUnit.Pixel, attributes);
        }
 
        return bmp;
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox1.Image = Image.FromFile("path_to_your_image.jpg"); // 设置PictureBox的图片
        fadeTimer.Start();
    }
}
复制代码

 

(评论功能已被禁用)
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2025年3月8日 星期六 【蛇】己卯月丙子日 乙巳年 二月初九 妇女节
您的IP:3.141.45.123,操作系统:未知操作系统,浏览器:未知浏览器
Copyright (C) 2000-2025 Lzhdim Software All Rights Reserved
点击右上角即可分享
微信分享提示