随笔 - 833  文章 - 1  评论 - 106  阅读 - 200万

C#中图片透明【转】

/// <summary>
/// 处理图片透明操作
/// </summary>
/// <param name="srcImage">原始图片</param>
/// <param name="opacity">透明度(0.0---1.0)</param>
/// <returns></returns>
private Image TransparentImage(Image srcImage, float 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);
    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);

    return resultImage;
}

ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。


ImageAttributes 对象包含有关在呈现时如何操作位图和图元文件颜色的信息。ImageAttributes 对象维护多个颜色调整设置,包括颜色调整矩阵、灰度调整矩阵、灰度校正值、颜色映射表和颜色阈值。呈现过程中,可以对颜色进行校正、调暗、调亮和移除。要应用这些操作,应初始化一个 ImageAttributes 对象,并将该 ImageAttributes 对象的路径(连同 Image 的路径)传递给 DrawImage 方法。

 

https://www.cnblogs.com/whisht/archive/2013/05/02/3085060.html

posted on   3D入魔  阅读(2557)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示