C#改变图片透明度的问题

C#中对颜色的调整是通过一个ColorMatrix的对象实现的这个对象表示一个5X5的矩阵用于对颜色进行线性的变换作为一般的理解只需要指定一个如下的矩阵即可实现对颜色的变... C#中对颜色的调整是通过一个ColorMatrix的对象实现的 这个对象表示一个5X5的矩阵 用于对颜色进行线性的变换 作为一般的理解 只需要指定一个如下的矩阵即可实现对颜色的变换:
1,0,0,0,0
0,1,0,0,0
0,0,1,0,0
0,0,0,透明度,0
0,0,0,0,1

简单的代码如下:
//注意using System.Drawing名字空间 opacity是想要设定的透明度

float[][] nArray ={ 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, opacity, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix matrix = new ColorMatrix(nArray);
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap )
Image srcImage = Image.FromFile("aaa.jpg");
Bitmap resultImage = new Bitmap(srcImage.Width, srcImage.Height);
Graphics g = Graphics.FromImage(resultImage);
g.DrawImage( srcImage, new Rectangle( 0, 0, srcImage.Width, srcImage.Height ), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel, attributes);

这是我搜到的一个方法,但是我对着代码不是很能看得懂,我要修改窗体中picturebox的一种图片的透明度,请问上述代码应该做怎样的修改
展开
 我来答
拽就该有个拽样
2015-01-16 · TA获得超过136个赞
知道答主
回答量:156
采纳率:0%
帮助的人:93.9万
展开全部
这很好理解嘛, 设置一个了透明度的矩阵, 然后添加到属性中, 从文件中载入了一个幅图像, 然后把图像绑定到了Graphics类中, 使用类中的DrawImage方法重新按之前设置的透明度属性绘制了图像.
更多追问追答
追问
你把上面代码的思路解释了一下,但是仅仅这段代码运行起来什么都没有发生,窗体上也看不到图片
追答
这个代码只是通过Graphics类对图像做了修改, 你想图像现实出来还要把g中的数据在picturebox中现实出来, 就好比你设置了一个变量n 让他等于了1 但是你想看到结果要自己显示出来, 明白了吧~
醉酒银虫
2015-01-16 · TA获得超过338个赞
知道小有建树答主
回答量:235
采纳率:0%
帮助的人:253万
展开全部

图片是可以透明的,但是控件背景透明不了。除非这样:

class CustomPictureBox : Control
{
    public CustomPictureBox()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint
                    | ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        this.BackColor = Color.Transparent;
        this.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
    }

    protected override CreateParams CreateParams
    {
        get
        {
            var result = base.CreateParams;
            result.ExStyle |= 0x00000020;
            return result;
        }
    }

    public Image Image { get; set; }
    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
        pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        float[][] nArray ={ 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.5f, 0},
                            new float[] {0, 0, 0, 0, 1}};
        ColorMatrix matrix = new ColorMatrix(nArray);
        ImageAttributes attributes = new ImageAttributes();
        attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        pe.Graphics.DrawImage(Image, new Rectangle(0, 0, this.Width, this.Height), 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, attributes);
    }
}

这个只是简单的实现,相当于PictureBox的Zoom模式,没有其他的功能。你可以在使用到PictureBox的地方用这个。

更多追问追答
追问
大神,控件背景不用透明,我想做的仅仅只是把已经半透明的图片在picturebox中显示出来,但是这段显示的代码我不会写
追答
后面加一句pictureBox1.Image = destImage;就行了然后把整个代码放在formload里面就行了。img文件路径是aaa.jpg
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式