利用C#實(shí)現(xiàn)修改圖片透明度功能
更新時(shí)間:2022年12月12日 15:03:33 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)修改圖片透明度功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
實(shí)踐過(guò)程
效果
代碼
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> /// 必需的設(shè)計(jì)器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設(shè)計(jì)器生成的代碼 /// <summary> /// 設(shè)計(jì)器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內(nèi)容。 /// </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 = "打開(kāi)圖片"; 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; }
到此這篇關(guān)于利用C#實(shí)現(xiàn)修改圖片透明度功能的文章就介紹到這了,更多相關(guān)C#修改圖片透明度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# WinForm實(shí)現(xiàn)鼠標(biāo)穿透功能
在WinForm開(kāi)發(fā)時(shí),會(huì)用到這樣一個(gè)場(chǎng)景,給屏幕增加水印Logo,但不影響畫(huà)面的操作,這里就會(huì)用到鼠標(biāo)穿透功能,下面我們就來(lái)學(xué)習(xí)一下鼠標(biāo)穿透功能的具體實(shí)現(xiàn)吧2023-11-11C#實(shí)現(xiàn)在控制臺(tái)輸出當(dāng)前系統(tǒng)時(shí)間的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在控制臺(tái)輸出當(dāng)前系統(tǒng)時(shí)間的方法,涉及C#時(shí)間函數(shù)DateTime.Now的使用方法,需要的朋友可以參考下2015-04-04C#統(tǒng)計(jì)C、C++及C#程序代碼行數(shù)的方法
這篇文章主要介紹了C#統(tǒng)計(jì)C、C++及C#程序代碼行數(shù)的方法,較為詳細(xì)的分析了C#統(tǒng)計(jì)文本文件的原理與相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#實(shí)現(xiàn)BBcode轉(zhuǎn)為Markdown的方法
這篇文章主要給大家介紹了關(guān)于C#實(shí)現(xiàn)BBcode轉(zhuǎn)Markdown的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02Unity3D Shader實(shí)現(xiàn)掃描顯示效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)掃描顯示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03