zoukankan      html  css  js  c++  java
  • C#设置图片透明度

    逐个像素进行Alpha值的设置,网上其他的代码不能处理有透明背景的图片,因此要对Alpha、R、G、B均为0的透明色进行特殊处理,不做转换。

     1         private Bitmap SetImageOpacity(Image srcImage, int opacity)
     2         {
     3             Bitmap pic = new Bitmap(srcImage);
     4             for (int w = 0; w < pic.Width; w++)
     5             {
     6                 for (int h = 0; h < pic.Height; h++)
     7                 {
     8                     Color c = pic.GetPixel(w, h);
     9                     Color newC;
    10                     if (!c.Equals(Color.FromArgb(0, 0, 0, 0)))
    11                     {
    12                         newC = Color.FromArgb(opacity, c);
    13                     }
    14                     else
    15                     {
    16                         newC = c;
    17                     }
    18                     pic.SetPixel(w, h, newC);
    19                 }
    20             }
    21             return pic;
    22         }
    23 
    24         private Image SetImageOpacity2(Image srcImage, int opacity)
    25         {
    26             Bitmap img = new Bitmap(srcImage);
    27             using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
    28             {
    29                 using (Graphics g = Graphics.FromImage(bmp))
    30                 {
    31                     g.DrawImage(img, 0, 0);
    32                     for (int h = 0; h <= img.Height - 1; h++)
    33                     {
    34                         for (int w = 0; w <= img.Width - 1; w++)
    35                         {
    36                             Color c = img.GetPixel(w, h);
    37                             if (!c.Equals(Color.FromArgb(0, 0, 0, 0)))
    38                             {
    39                                 bmp.SetPixel(w, h, Color.FromArgb(opacity, c.R, c.G, c.B));
    40                             }
    41                             else
    42                             {
    43                                 bmp.SetPixel(w, h, Color.FromArgb(c.A, c.R, c.G, c.B));
    44                             }
    45                         }
    46                     }
    47                 }
    48                 return (Image)bmp.Clone();
    49             }
    50         }
  • 相关阅读:
    大端小端与数字的二进制存储
    java基础之进制转换汇总
    (转) tcp udp通讯协议
    JAVA Tcp Udp的通讯实现(转)
    ExecutorService创建线程使用 转()
    转:java中的位运算
    SVN服务器的搭建与TortoiseSVN的使用
    [Mark]VM Cone & Template
    [Mark]VM migrate
    [Mark] ethtool command in REHL OS
  • 原文地址:https://www.cnblogs.com/wyp1988/p/9843430.html
Copyright © 2011-2022 走看看