• Bitmap透明图片描边


      1         public static byte[] ToArray(this Bitmap bitmap)
      2         {
      3             var lockbits = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
      4             byte[] bs = new byte[lockbits.Stride * lockbits.Height];
      5             Marshal.Copy(lockbits.Scan0, bs, 0, lockbits.Stride * lockbits.Height);
      6             return bs;
      7         }
      8 
      9         /// <summary>
     10         /// 获取透明边缘坐标
     11         /// </summary>
     12         /// <param name="bitmap"></param>
     13         /// <param name="overflow">是否允许坐标超出图片范围</param>
     14         /// <returns></returns>
     15         public static List<Point> GetVerge(this Bitmap bitmap, bool overflow = false)
     16         {
     17             List<Point> ps = new List<Point>();
     18             byte[] arr = bitmap.ToArray();
     19             int w = bitmap.Width;
     20             int h = bitmap.Height;
     21             int x = 0;
     22             int y = 0;
     23             int p = 3;
     24             int s = w * 4;
     25 
     26             #region x = 0
     27             #region y = 0
     28             if(arr[p] == 0)
     29             {
     30                 if (arr[p + 4] > 0 ||
     31                     arr[p + s] > 0 || arr[p + s + 4] > 0)
     32                     ps.Add(new Point(x, y));
     33             }
     34             else if (overflow)
     35             {
     36                 ps.Add(new Point(-1, -1));
     37                 ps.Add(new Point(0, -1));
     38                 ps.Add(new Point(1, -1));
     39                 ps.Add(new Point(-1, 0));
     40                 ps.Add(new Point(-1, 1));
     41             }
     42             #endregion
     43             #region 0<y<h-1
     44             for (y = 1; y < h - 1; y++)
     45             {
     46                 p = 3 + s * y;
     47                 if(arr[p] == 0)
     48                 {
     49                     if (arr[p - s] > 0 || arr[p - s + 4] > 0 ||
     50                         arr[p + 4] > 0 ||
     51                         arr[p + s] > 0 || arr[p + s + 4] > 0)
     52                         ps.Add(new Point(x, y));
     53                 }
     54                 else if (overflow)
     55                 {
     56                     ps.Add(new Point(-1, y - 1));
     57                     ps.Add(new Point(-1, y));
     58                     ps.Add(new Point(-1, y + 1));
     59                 }
     60             }
     61             #endregion
     62             #region y = h - 1
     63             p = 3 + s * y;
     64             if (arr[p] == 0)
     65             {
     66                 if (arr[p - s] > 0 || arr[p - s + 4] > 0 ||
     67                     arr[p + 4] > 0)
     68                     ps.Add(new Point(x, y));
     69             }
     70             else if (overflow)
     71             {
     72                 ps.Add(new Point(-1, y - 1));
     73                 ps.Add(new Point(-1, y));
     74                 ps.Add(new Point(-1, y + 1));
     75                 ps.Add(new Point(0, y + 1));
     76                 ps.Add(new Point(1, y + 1));
     77             }
     78             #endregion
     79             #endregion
     80 
     81             #region 0<x<w-1
     82             for (x = 1; x < w - 1; x++)
     83             {
     84                 #region y = 0
     85                 y = 0;
     86                 p = x * 4 + 3;
     87                 if (arr[p] == 0)
     88                 {
     89                     if (arr[p - 4] > 0 || arr[p + 4] > 0 ||
     90                         arr[p + s - 4] > 0 || arr[p + s] > 0 || arr[p + s + 4] > 0)
     91                         ps.Add(new Point(x, y));
     92                 }
     93                 else if (overflow)
     94                 {
     95                     ps.Add(new Point(x - 1, y - 1));
     96                     ps.Add(new Point(x, y - 1));
     97                     ps.Add(new Point(x + 1, y - 1));
     98                 }
     99                 #endregion
    100                 #region 0<y<h-1
    101                 for (y = 1; y < h - 1; y++)
    102                 {
    103                     p = x * 4 + 3 + s * y;
    104                     if (arr[p] == 0)
    105                     {
    106                         if (arr[p - s - 4] > 0 || arr[p - s] > 0 || arr[p - s + 4] > 0 ||
    107                             arr[p - 4] > 0 || arr[p + 4] > 0 ||
    108                             arr[p + s - 4] > 0 || arr[p + s] > 0 || arr[p + s + 4] > 0)
    109                             ps.Add(new Point(x, y));
    110                     }
    111                 }
    112                 #endregion
    113                 #region y = h - 1
    114                 p = x * 4 + 3 + s * y;
    115                 if (arr[p] == 0)
    116                 {
    117                     if (arr[p - s - 4] > 0 || arr[p - s] > 0 || arr[p - s + 4] > 0 ||
    118                         arr[p - 4] > 0 || arr[p + 4] > 0)
    119                         ps.Add(new Point(x, y));
    120                 }
    121                 else if (overflow)
    122                 {
    123                     ps.Add(new Point(x - 1, y + 1));
    124                     ps.Add(new Point(x, y + 1));
    125                     ps.Add(new Point(x + 1, y + 1));
    126                 }
    127                 #endregion
    128             }
    129             #endregion
    130 
    131             #region x = w - 1
    132             #region y = 0
    133             x = w - 1;
    134             y = 0;
    135             p = x * 4 + 3;
    136             if (arr[p] == 0)
    137             {
    138                 if (arr[p - 4] > 0 ||
    139                     arr[p + s - 4] > 0 || arr[p + s] > 0)
    140                     ps.Add(new Point(x, y));
    141             }
    142             else if (overflow)
    143             {
    144                 ps.Add(new Point(x - 1, y - 1));
    145                 ps.Add(new Point(x, y - 1));
    146                 ps.Add(new Point(x + 1, y - 1));
    147                 ps.Add(new Point(x + 1, y));
    148                 ps.Add(new Point(x + 1, y + 1));
    149             }
    150             #endregion
    151             #region 0<y<h-1
    152             for (y = 1; y < h - 1; y++)
    153             {
    154                 p = x * 4 + 3 + s * y;
    155                 if (arr[p] == 0)
    156                 {
    157                     if (arr[p - s - 4] > 0 || arr[p - s] > 0 ||
    158                         arr[p - 4] > 0 ||
    159                         arr[p + s - 4] > 0 || arr[p + s] > 0)
    160                         ps.Add(new Point(x, y));
    161                 }
    162                 else if (overflow)
    163                 {
    164                     ps.Add(new Point(x + 1, y - 1));
    165                     ps.Add(new Point(x + 1, y));
    166                     ps.Add(new Point(x + 1, y + 1));
    167                 }
    168             }
    169             #endregion
    170             #region y = h - 1
    171             p = x * 4 + 3 + s * y;
    172             if (arr[p] == 0)
    173             {
    174                 if (arr[p - s - 4] > 0 || arr[p - s] > 0 ||
    175                     arr[p - 4] > 0)
    176                     ps.Add(new Point(x, y));
    177             }
    178             else if (overflow)
    179             {
    180                 ps.Add(new Point(x + 1, y - 1));
    181                 ps.Add(new Point(x + 1, y));
    182                 ps.Add(new Point(x - 1, y + 1));
    183                 ps.Add(new Point(x, y + 1));
    184                 ps.Add(new Point(x + 1, y + 1));
    185             }
    186             #endregion
    187             #endregion
    188             return ps;
    189         }
    190 
    191         /// <summary>
    192         /// 获取透明边缘坐标,返回只有描边的图片
    193         /// </summary>
    194         /// <param name="bitmap"></param>
    195         /// <param name="overflow">是否允许坐标超出图片范围</param>
    196         /// <returns></returns>
    197         public static byte[] GetVerge1(this Bitmap bitmap, bool overflow = false)
    198         {
    199             int w = overflow ? bitmap.Width + 2 : bitmap.Width;
    200             int h = overflow ? bitmap.Height + 2 : bitmap.Height;
    201             int s = w * 4;
    202             byte[] bs = new byte[s * h];
    203             List<Point> ps = bitmap.GetVerge(overflow);
    204             for (int i = 0; i < ps.Count; i++)
    205             {
    206                 int x = overflow ? ps[i].X + 1 : ps[i].X;
    207                 int y = overflow ? ps[i].Y + 1 : ps[i].Y;
    208                 bs[x * 4 + s * y + 1] = 255;
    209                 bs[x * 4 + s * y + 3] = 255;
    210             }
    211             return bs;
    212         }
  • 相关阅读:
    小球掉落
    String当中与转换相关常用的方法有
    字符串的截取方法
    golang 管道
    golang--协程之间通信的方式
    golang--goroutine
    go 时间操作
    吉格勒定理
    检视阅读
    git branch -a发现分支显示不全
  • 原文地址:https://www.cnblogs.com/RedSky/p/16087830.html
Copyright © 2020-2023  润新知