تغییر Opacity عکس PictureBox
با فرض اینکه یک عکس در درایو :C داریم و میخواهیم Opacity آن را تغییر دهیم از کد زیر استفاده می کنیم :
using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Imaging; namespace ChangeOpacityPicture { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static Bitmap ChangeOpacity(Image image, float opacityvalue) { Bitmap Bitmap1 = new Bitmap(image.Width, image.Height); Graphics graphics = Graphics.FromImage(Bitmap1); ColorMatrix colormatrix = new ColorMatrix(); colormatrix.Matrix33 = opacityvalue; ImageAttributes ImageAttribute = new ImageAttributes(); ImageAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); graphics.DrawImage(image, new Rectangle(0, 0, Bitmap1.Width, Bitmap1.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, ImageAttribute); graphics.Dispose(); return Bitmap1; } private void Form1_Load(object sender, EventArgs e) { pictureBox1.Image = ChangeOpacity(Image.FromFile(@"C:\Koala.jpg"), (0.5f)); } } }