利用C#实现修改图片透明度功能

 更新时间:2022年12月12日 15:03:33   作者:芝麻粒儿  
这篇文章主要为大家详细介绍了如何利用C#实现修改图片透明度功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以了解一下

实践过程

效果

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Image new_img;
    private void ChangeAlpha()
    {
        pictureBox1.Refresh();
        Bitmap source = new Bitmap(pictureBox1.Image);
        Bitmap effect = new Bitmap(pictureBox1.Image.Width,pictureBox1.Image.Height);
        Graphics _effect = Graphics.FromImage(effect);
        float[][] matrixItems ={new float[]{1,0,0,0,0},
                                  new float [] {0,1,0,0,0},
                                  new float []{0,0,1,0,0},
                                  new float []{0,0,0,0,0},
                                  new float[]{0,0,0,trackBar1.Value/255f,1}};
        ColorMatrix imgMatrix = new ColorMatrix(matrixItems);
        ImageAttributes imgEffect = new ImageAttributes();
        imgEffect.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        if (source.Width <= 368)
        {
            _effect.DrawImage(source, new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height), 0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height, GraphicsUnit.Pixel, imgEffect);
        }
        else
        {
            _effect.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height), 0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height, GraphicsUnit.Pixel, imgEffect);
        }
        pictureBox1.Image = effect;
    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void trackBar1_ValueChanged(object sender, EventArgs e)
    {
        ChangeAlpha();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
        }
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.trackBar1 = new System.Windows.Forms.TrackBar();
        this.button1 = new System.Windows.Forms.Button();
        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.Location = new System.Drawing.Point(12, 80);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(424, 258);
        this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        // 
        // trackBar1
        // 
        this.trackBar1.Location = new System.Drawing.Point(12, 41);
        this.trackBar1.Maximum = 100;
        this.trackBar1.Minimum = 1;
        this.trackBar1.Name = "trackBar1";
        this.trackBar1.Size = new System.Drawing.Size(424, 45);
        this.trackBar1.TabIndex = 1;
        this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.None;
        this.trackBar1.Value = 100;
        this.trackBar1.ValueChanged += new System.EventHandler(this.trackBar1_ValueChanged);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(12, 12);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 2;
        this.button1.Text = "打开图片";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // openFileDialog1
        // 
        this.openFileDialog1.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(447, 350);
        this.Controls.Add(this.pictureBox1);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.trackBar1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.TrackBar trackBar1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.OpenFileDialog openFileDialog1;
}

到此这篇关于利用C#实现修改图片透明度功能的文章就介绍到这了,更多相关C#修改图片透明度内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C# 8.0新特性介绍

    C# 8.0新特性介绍

    C# 语言是在2000发布的,至今已正式发布了7个版本,每个版本都包含了许多令人兴奋的新特性和功能更新。下面通过本文给大家分享下C# 8.0的三个令人兴奋的新特性,需要的朋友参考下吧
    2017-10-10
  • C#使用Sleep(Int32)方法实现动态显示时间

    C#使用Sleep(Int32)方法实现动态显示时间

    这篇文章主要为大家详细介绍了C#如何使用Sleep(Int32)方法实现动态显示时间,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2024-01-01
  • C# SimpleJSON字典反序列化实战教程

    C# SimpleJSON字典反序列化实战教程

    C# 解析JSON道路千万条,其中最流行的还是由Newtonsoft出品的Json.NET,只需要将Json的结构做成C#的class,即可实现完美的序列化和反序列化,这篇文章主要介绍了C# SimpleJSON字典反序列化实战教程,需要的朋友可以参考下
    2024-03-03
  • C#画笔Pen保存和恢复图形对象的设置方法

    C#画笔Pen保存和恢复图形对象的设置方法

    这篇文章主要介绍了C#画笔Pen保存和恢复图形对象的设置方法,实例分析了画笔的保存save及恢复属性Restore的相关使用技巧,需要的朋友可以参考下
    2015-06-06
  • C#面向对象编程中接口隔离原则的示例详解

    C#面向对象编程中接口隔离原则的示例详解

    在面向对象编程中,SOLID 是五个设计原则的首字母缩写,旨在使软件设计更易于理解、灵活和可维护。本文将通过实例详细讲讲C#面向对象编程中接口隔离原则,需要的可以参考一下
    2022-07-07
  • C#实现将HTML文档和HTML字符串转换为图片

    C#实现将HTML文档和HTML字符串转换为图片

    在.NET开发中,将HTML内容转换为图片的需求广泛存在于报告生成,邮件内容存档,网页快照等场景,下面我们就来看看HTML文档和字符串转图片两种场景的转换方案吧
    2025-06-06
  • C#中lock死锁实例教程

    C#中lock死锁实例教程

    这篇文章主要介绍了C#中lock死锁的用法,对于共享资源的访问及C#程序设计的安全性而言,有着非常重要的意义!需要的朋友可以参考下
    2014-08-08
  • C#使用MQTTnet实现服务端与客户端的通讯的示例

    C#使用MQTTnet实现服务端与客户端的通讯的示例

    本文主要介绍了C#使用MQTTnet实现服务端与客户端的通讯的示例,包括协议特性、连接管理、QoS机制和安全策略,具有一定的参考价值,感兴趣的可以了解一下
    2025-05-05
  • Unity实现旋转扭曲图像特效

    Unity实现旋转扭曲图像特效

    这篇文章主要为大家详细介绍了Unity实现旋转扭曲图像特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#常用目录文件操作类实例

    C#常用目录文件操作类实例

    这篇文章主要介绍了C#常用目录文件操作类,实例分析了C#针对目录的读取、检测及查找等相关操作技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03

最新评论