zoukankan      html  css  js  c++  java
  • 图片处理类 类库--C#

    调用如下:

    [csharp] view plain copy
     
     print?
    1. Bitmap bitmap = new Bitmap("C:\Users\Thinkpad\Desktop\aa.jpg");  
    2.            Bitmap[] bit = new Bitmap[13];  
    3.            for (int i = 0; i < 13; i++) {  
    4.                bit[i] = new Bitmap("C:\Users\Thinkpad\Desktop\aa.jpg");  
    5.            }  
    6.            //去色  
    7.            pictureEdit1.Image = ImagePS.DeColor(bit[0]);  
    8.            //去色 去掉白色  
    9.            pictureEdit2.Image = ImagePS.DeColor(bit[1], Color.White);  
    10.            //底片  
    11.            pictureEdit3.Image = ImagePS.ReImg(bit[2]);  
    12.            //浮雕  
    13.            pictureEdit4.Image = ImagePS.Raised(bit[3]);             
    14.            //高斯模糊  
    15.            pictureEdit5.Image = ImagePS.Softness(bit[4]);  
    16.            //锐化  
    17.            pictureEdit6.Image = ImagePS.Definition(bit[5]);  
    18.            //色相  
    19.            pictureEdit7.Image = ImagePS.SetColorFilter(bit[6], ImagePS.ColorFilterType.Red);  
    20.            //对比度  
    21.            pictureEdit8.Image = ImagePS.Contrast(bit[7], 50);  
    22.            //缩放  
    23.            pictureEdit9.Image = ImagePS.KiResizeImage(bit[8], 100, 100);  
    24.            //亮度调整  
    25.            pictureEdit10.Image = ImagePS.LightImage(bit[9], -100);  
    26.            //设置曲线  
    27.            pictureEdit11.Image = ImagePS.SetGamma(bit[10],250,100,250);  
    28.            //得到纯色图片  
    29.            pictureEdit12.Image = ImagePS.GetColorImg(Color.Yellow,100,100);  
    [csharp] view plain copy
     
     print?
    1. <strong><span style="font-size:14px;">底层类:</span></strong>  
    [csharp] view plain copy
     
     print?
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using System.Drawing;  
    5. using System.Drawing.Imaging;  
    6. using System.Drawing.Drawing2D;  
    7. using System.Windows.Forms;  
    8.   
    9. namespace Common  
    10. {  
    11.     /// <summary>  
    12.     /// 图片处理 基础类  
    13.     /// </summary>  
    14.     public class ImagePS  
    15.     {  
    16.         public static Bitmap GetControlBmp(Control ctrl, int offsetX, int offsetY)  
    17.         {  
    18.             Graphics myGraphics = ctrl.CreateGraphics();  
    19.             Rectangle r = ctrl.RectangleToScreen(ctrl.Bounds);  
    20.             Bitmap memoryImage = new Bitmap(r.Width, r.Height, myGraphics);  
    21.             Graphics memoryGraphics = Graphics.FromImage(memoryImage);  
    22.             memoryGraphics.CopyFromScreen(r.X - offsetX, r.Y - offsetY, 0, 0, new Size(r.Width, r.Height));  
    23.             return memoryImage;  
    24.         }  
    25.         /// <summary>  
    26.         /// 色相类型,红,绿,蓝  
    27.         /// </summary>  
    28.         public enum ColorFilterType  
    29.         {  
    30.             Red, Green, Blue  
    31.         }  
    32.   
    33.         /// <summary>  
    34.         /// 底片效果  
    35.         /// </summary>  
    36.         /// <param name="img">图片</param>  
    37.         /// <returns>处理后的图片</returns>  
    38.         public static Bitmap ReImg(Bitmap img)  
    39.         {  
    40.             byte r, g, b;  
    41.             for (int i = 0; i < img.Width; i++)  
    42.             {  
    43.                 for (int j = 0; j < img.Height; j++)  
    44.                 {  
    45.                     r = (byte)(255 - img.GetPixel(i, j).R);  
    46.                     g = (byte)(255 - img.GetPixel(i, j).G);  
    47.                     b = (byte)(255 - img.GetPixel(i, j).B);  
    48.   
    49.                     img.SetPixel(i, j, Color.FromArgb(r, g, b));  
    50.                 }  
    51.             }  
    52.   
    53.             return img;  
    54.         }  
    55.   
    56.         /// <summary>  
    57.         /// 图片亮度调整  
    58.         /// </summary>  
    59.         /// <param name="bitmap">图片</param>  
    60.         /// <param name="light">亮度[-255,255]</param>  
    61.         /// <returns>处理后的图片</returns>  
    62.         public static Bitmap LightImage(Bitmap bitmap, int light)  
    63.         {  
    64.             light = light > 255 ? 255 : light;  
    65.             light = light < -255 ? -255 : light;  
    66.   
    67.             int w = bitmap.Width;  
    68.             int h = bitmap.Height;  
    69.             int pix = 0;  
    70.             BitmapData data = bitmap.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);  
    71.   
    72.             unsafe  
    73.             {  
    74.                 byte* p = (byte*)data.Scan0;  
    75.                 int offset = data.Stride - w * 3;  
    76.                 for (int i = 0; i < w; i++)  
    77.                 {  
    78.                     for (int j = 0; j < h; j++)  
    79.                     {  
    80.                         for (int x = 0; x < 3; x++)  
    81.                         {  
    82.                             pix = p[x] + light;  
    83.                             if (light < 0)  
    84.                                 p[x] = (byte)Math.Max(0, pix);  
    85.                             if (light > 0)  
    86.                                 p[x] = (byte)Math.Min(255, pix);  
    87.                         }  
    88.                         p += 3;  
    89.                     }  
    90.                     p += offset;  
    91.                 }  
    92.             }  
    93.   
    94.             bitmap.UnlockBits(data);  
    95.             return bitmap;  
    96.         }  
    97.   
    98.         /// <summary>   
    99.         /// 图片去色  
    100.         /// </summary>  
    101.         /// <param name="img">要去色的图片</param>  
    102.         /// <returns>处理后的图片</returns>  
    103.         public static Bitmap DeColor(Bitmap img)  
    104.         {  
    105.             if (img == null)  
    106.                 return img;  
    107.   
    108.             int w = img.Width, h = img.Height;  
    109.             Color oldColor, newColor;  
    110.             int y = 0;  
    111.   
    112.             for (int i = 0; i < w; i++)  
    113.             {  
    114.                 for (int j = 0; j < h; j++)  
    115.                 {  
    116.                     oldColor = img.GetPixel(i, j);  
    117.                     byte r = oldColor.R;  
    118.                     byte g = oldColor.G;  
    119.                     byte b = oldColor.B;  
    120.                     y = (r + g + b) / 3;  
    121.   
    122.                     newColor = Color.FromArgb(y, y, y);  
    123.                     img.SetPixel(i, j, newColor);  
    124.                 }  
    125.             }  
    126.             return img;  
    127.         }  
    128.         /// <summary>  
    129.         /// 图片去色  
    130.         /// </summary>  
    131.         /// <param name="img">要去色的图片</param>  
    132.         /// <param name="c">要去掉的颜色</param>  
    133.         /// <returns>处理后的图片</returns>  
    134.         public static Bitmap DeColor(Bitmap img, Color c)  
    135.         {  
    136.             int w = img.Width, h = img.Height;  
    137.             Color oldColor;             
    138.             int cha1 = 60;  
    139.             int cha2 = 60;  
    140.   
    141.             for (int i = 0; i < w; i++)  
    142.             {  
    143.                 for (int j = 0; j < h; j++)  
    144.                 {  
    145.                     oldColor = img.GetPixel(i, j);  
    146.                     byte r = oldColor.R;  
    147.                     byte g = oldColor.G;  
    148.                     byte b = oldColor.B;  
    149.   
    150.                     bool bol1 = false, bol2 = false, bol3 = false;  
    151.                     if (r - c.R < cha1 && r - c.R > -cha2)  
    152.                         bol1 = true;  
    153.                     if (g - c.G < cha1 && g - c.G > -cha2)  
    154.                         bol2 = true;  
    155.   
    156.                     if (b - c.B < cha1 && b - c.B > -cha2)  
    157.                         bol3 = true;  
    158.   
    159.                     if (bol1 && bol2 && bol3)  
    160.                     {  
    161.                         //y = (r + g + b) / 3;  
    162.                         //newColor = Color.FromArgb(y, y, y);  
    163.                         img.SetPixel(i, j, Color.Black);  
    164.                     }  
    165.                 }  
    166.             }  
    167.   
    168.             return img;  
    169.         }  
    170.   
    171.         /// <summary>  
    172.         /// 浮雕效果  
    173.         /// </summary>  
    174.         /// <param name="img">要处理的图片</param>  
    175.         /// <returns>处理后的图片</returns>  
    176.         public static Bitmap Raised(Bitmap img)  
    177.         {  
    178.             Color pix1, pix2;  
    179.   
    180.             for (int x = 0; x < img.Width - 1; x++)  
    181.             {  
    182.                 for (int y = 0; y < img.Height - 1; y++)  
    183.                 {  
    184.                     pix1 = img.GetPixel(x, y);  
    185.                     pix2 = img.GetPixel(x + 1, y + 1);  
    186.   
    187.                     int r = 0, g = 0, b = 0;//新颜色的R,G,B  
    188.                     r = Math.Abs(pix1.R - pix2.R + 100);  
    189.                     g = Math.Abs(pix1.G - pix2.G + 100);  
    190.                     b = Math.Abs(pix1.B - pix2.B + 100);  
    191.   
    192.                     //控制上下限 0 -- 255  
    193.                     r = Math.Min(255, r);  
    194.                     g = Math.Min(255, g);  
    195.                     b = Math.Min(255, b);  
    196.   
    197.                     r = Math.Max(0, r);  
    198.                     g = Math.Max(0, g);  
    199.                     b = Math.Max(0, b);  
    200.   
    201.                     img.SetPixel(x, y, Color.FromArgb(r, g, b));  
    202.                 }  
    203.             }  
    204.   
    205.             return img;  
    206.         }  
    207.   
    208.         /// <summary>  
    209.         /// 高斯模糊  
    210.         /// </summary>  
    211.         /// <param name="img">要处理的图片</param>  
    212.         /// <returns>处理后的图片</returns>  
    213.         public static Bitmap Softness(Bitmap img)  
    214.         {  
    215.             Color pixel;  
    216.             int Width = img.Width;  
    217.             int Height = img.Height;  
    218.             int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };  
    219.   
    220.             for (int x = 1; x < Width - 1; x++)  
    221.             {  
    222.                 for (int y = 1; y < Height - 1; y++)  
    223.                 {  
    224.                     int r = 0, g = 0, b = 0;  
    225.                     int Index = 0;  
    226.                     for (int col = -1; col <= 1; col++)  
    227.                     {  
    228.                         for (int row = -1; row <= 1; row++)  
    229.                         {  
    230.                             pixel = img.GetPixel(x + row, y + col);  
    231.                             r += pixel.R * Gauss[Index];  
    232.                             g += pixel.G * Gauss[Index];  
    233.                             b += pixel.B * Gauss[Index];  
    234.                             Index++;  
    235.                         }  
    236.                     }  
    237.                     r /= 16;  
    238.                     g /= 16;  
    239.                     b /= 16;  
    240.                     //处理颜色值溢出  
    241.                     r = r > 255 ? 255 : r;  
    242.                     r = r < 0 ? 0 : r;  
    243.                     g = g > 255 ? 255 : g;  
    244.                     g = g < 0 ? 0 : g;  
    245.                     b = b > 255 ? 255 : b;  
    246.                     b = b < 0 ? 0 : b;  
    247.                     img.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));  
    248.                 }  
    249.             }  
    250.             return img;  
    251.         }  
    252.   
    253.         /// <summary>  
    254.         /// 对比度  
    255.         /// </summary>  
    256.         /// <param name="img">要处理的图片</param>  
    257.         /// <param name="m">对比度[-100,100]</param>  
    258.         /// <returns>处理后的图片</returns>  
    259.         public static Bitmap Contrast(Bitmap img, int m)  
    260.         {  
    261.             if (m < -100) m = -100;  
    262.             if (m > 100) m = 100;  
    263.             //Width, Height  
    264.             int w = img.Width;  
    265.             int h = img.Height;  
    266.             double pix = 0;  
    267.             BitmapData data = img.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);  
    268.             int offset = data.Stride - 3 * w;  
    269.             double contrast = (100.00 + m) / 100.00;  
    270.             contrast *= contrast;  
    271.   
    272.             unsafe  
    273.             {  
    274.                 byte* p = (byte*)data.Scan0;  
    275.                 for (int i = 0; i < w; i++)  
    276.                 {  
    277.                     for (int j = 0; j < h; j++)  
    278.                     {  
    279.                         for (int y = 0; y < 3; y++)  
    280.                         {  
    281.                             pix = ((p[y] / 255.00 - 0.5) * contrast + 0.5) * 255;  
    282.                             if (pix < 0) pix = 0;  
    283.                             if (pix > 255) pix = 255;  
    284.                             p[y] = (byte)pix;  
    285.                         }  
    286.                         p += 3;  
    287.                     }  
    288.                     p += offset;  
    289.                 }  
    290.             }  
    291.   
    292.             img.UnlockBits(data);  
    293.             return img;  
    294.         }  
    295.   
    296.         /// <summary>  
    297.         /// 锐化  
    298.         /// </summary>  
    299.         /// <param name="img">要处理的图片</param>  
    300.         /// <returns>处理后的图片</returns>  
    301.         public static Bitmap Definition(Bitmap img)  
    302.         {  
    303.             int w = img.Width;  
    304.             int h = img.Height;  
    305.             Color c;  
    306.             int[] laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };  
    307.   
    308.             for (int i = 1; i < w - 1; i++)  
    309.             {  
    310.                 for (int j = 1; j < h - 1; j++)  
    311.                 {  
    312.                     int r = 0, g = 0, b = 0;  
    313.                     int index = 0;  
    314.   
    315.                     for (int col = -1; col <= 1; col++)  
    316.                     {  
    317.                         for (int row = -1; row <= 1; row++)  
    318.                         {  
    319.                             c = img.GetPixel(i + row, j + col);  
    320.                             r += c.R * laplacian[index];  
    321.                             g += c.G * laplacian[index];  
    322.                             b += c.B * laplacian[index];  
    323.   
    324.                             index += 1;  
    325.                         }  
    326.                     }  
    327.   
    328.                     r = Math.Max(0, r);  
    329.                     r = Math.Min(255, r);  
    330.                     g = Math.Max(0, g);  
    331.                     g = Math.Min(255, g);  
    332.                     b = Math.Max(0, b);  
    333.                     b = Math.Min(255, b);  
    334.   
    335.                     img.SetPixel(i - 1, j - 1, Color.FromArgb(r, g, b));  
    336.                 }//end for  
    337.             }//end for  
    338.   
    339.             return img;  
    340.         }  
    341.   
    342.         /// <summary>  
    343.         /// 雾化  
    344.         /// </summary>  
    345.         /// <param name="img">要处理的图片</param>  
    346.         /// <returns>处理后的图片</returns>  
    347.         public static Bitmap Atomization(Bitmap img)  
    348.         {  
    349.             int w = img.Width;  
    350.             int h = img.Height;  
    351.             Color pix;  
    352.             //在此实例化,雾化效果  
    353.             Random ran = new Random();  
    354.   
    355.             for (int x = 0; x < w; x++)  
    356.             {  
    357.                 for (int y = 0; y < h; y++)  
    358.                 {  
    359.                     //Random ran = new Random(); 在此实例化,水一样的效果  
    360.                     int dx = x + ran.Next(1234567) % 17;  
    361.                     int dy = y + ran.Next(1234567) % 17;  
    362.                     pix = img.GetPixel(Math.Min(dx, w - 1), Math.Min(dy, h - 1));  
    363.   
    364.                     img.SetPixel(x, y, pix);  
    365.                 }  
    366.             }  
    367.   
    368.             return img;  
    369.         }  
    370.   
    371.         /// <summary>  
    372.         /// 设置色相  
    373.         /// </summary>  
    374.         /// <param name="img">要处理的图片</param>  
    375.         /// <param name="cType">色相</param>  
    376.         /// <returns>处理后的图片</returns>  
    377.         public static Bitmap SetColorFilter(Bitmap img, ColorFilterType cType)  
    378.         {  
    379.             Color c;  
    380.             int r = 0, g = 0, b = 0;  
    381.   
    382.             for (int x = 0; x < img.Width; x++)  
    383.             {  
    384.                 for (int y = 0; y < img.Height; y++)  
    385.                 {  
    386.                     c = img.GetPixel(x, y);  
    387.                     r = c.R;  
    388.                     g = c.G;  
    389.                     b = c.B;  
    390.   
    391.                     if (cType == ColorFilterType.Red)  
    392.                     {  
    393.                         g -= 255;  
    394.                         b -= 255;  
    395.                     }  
    396.                     else if (cType == ColorFilterType.Green)  
    397.                     {  
    398.                         r -= 255;  
    399.                         b -= 255;  
    400.                     }  
    401.                     else  
    402.                     {  
    403.                         r -= 255;  
    404.                         g -= 255;  
    405.                     }  
    406.   
    407.                     //控制色值大小 0 -- 255  
    408.                     r = Math.Max(0, r);  
    409.                     g = Math.Max(0, g);  
    410.                     b = Math.Max(0, b);  
    411.                     r = Math.Min(255, r);  
    412.                     g = Math.Min(255, g);  
    413.                     b = Math.Min(255, b);  
    414.   
    415.                     img.SetPixel(x, y, Color.FromArgb(r, g, b));  
    416.                 }  
    417.             }  
    418.   
    419.             return img;  
    420.         }  
    421.   
    422.         /// <summary>  
    423.         /// 曲线  
    424.         /// </summary>  
    425.         /// <param name="img">要处理的图片</param>  
    426.         /// <param name="red">红</param>  
    427.         /// <param name="green">绿</param>  
    428.         /// <param name="blue">蓝</param>  
    429.         /// <returns>处理后的图片</returns>  
    430.         public static Bitmap SetGamma(Bitmap img, byte red, byte green, byte blue)  
    431.         {  
    432.             Color c;  
    433.             byte[] reds = CreateGamma(red);  
    434.             byte[] greens = CreateGamma(green);  
    435.             byte[] blues = CreateGamma(blue);  
    436.             for (int x = 0; x < img.Width; x++)  
    437.             {  
    438.                 for (int y = 0; y < img.Height; y++)  
    439.                 {  
    440.                     c = img.GetPixel(x, y);  
    441.                     img.SetPixel(x, y, Color.FromArgb(reds[c.R], greens[c.G], blues[c.B]));  
    442.                 }  
    443.             }  
    444.   
    445.             return img;  
    446.         }  
    447.   
    448.         //创建 曲线数组  
    449.         private static byte[] CreateGamma(byte color)  
    450.         {  
    451.             byte[] gammas = new byte[256];  
    452.             for (int i = 0; i < 256; i++)  
    453.             {  
    454.                 gammas[i] = Math.Min((byte)255, (byte)(255.0F * Math.Pow(i / 255, 1.0F / color) + 0.5F));  
    455.             }  
    456.   
    457.             return gammas;  
    458.         }  
    459.   
    460.   
    461.         /// <summary>  
    462.         /// 合并图片  
    463.         /// </summary>  
    464.         /// <param name="imgs">要处理的图片列表</param>  
    465.         /// <param name="z">图片间隔</param>  
    466.         /// <returns></returns>  
    467.         public static Bitmap MergerImg(List<Bitmap> imgs, int z)  
    468.         {  
    469.             if (imgs.Count <= 0)  
    470.                 return null;  
    471.   
    472.             int w = 0;  
    473.             foreach (Bitmap bmp in imgs)  
    474.                 if (bmp != null)  
    475.                     w = w + bmp.Width;  
    476.   
    477.             w += z * (imgs.Count - 1);  
    478.   
    479.             int h = GetMaxHeight(imgs);  
    480.   
    481.             return MergerImg(imgs, z, w, h);  
    482.         }  
    483.   
    484.   
    485.         private static int GetMaxHeight(List<Bitmap> imgs)  
    486.         {  
    487.             if (imgs == null || imgs.Count == 0)  
    488.                 return 0;  
    489.   
    490.             int maxHeight = 0;  
    491.             foreach (Bitmap bmp in imgs)  
    492.             {  
    493.                 if (bmp == null)  
    494.                     continue;  
    495.   
    496.                 if (maxHeight == -1)  
    497.                 {  
    498.                     maxHeight = bmp.Height;  
    499.                 }  
    500.                 else  
    501.                     maxHeight = bmp.Height > maxHeight ? bmp.Height : maxHeight;  
    502.             }  
    503.   
    504.             return maxHeight;  
    505.         }  
    506.   
    507.         private static int GetMinHeight(List<Bitmap> imgs)  
    508.         {  
    509.             if (imgs == null || imgs.Count == 0)  
    510.                 return 0;  
    511.   
    512.             int mixHeight = 0;  
    513.             foreach (Bitmap bmp in imgs)  
    514.             {  
    515.                 if (mixHeight == 0)  
    516.                 {  
    517.                     mixHeight = bmp.Height;  
    518.                 }  
    519.                 else  
    520.                     mixHeight = bmp.Height < mixHeight ? bmp.Height : mixHeight;  
    521.             }  
    522.             return mixHeight;  
    523.         }  
    524.   
    525.         private static Bitmap MergerImg(List<Bitmap> imgs, int z, int w, int h)  
    526.         {  
    527.             //创建要显示的图片对象,根据参数的个数设置宽度  
    528.             Bitmap backgroudImg = new Bitmap(w, h);  
    529.             Graphics g = Graphics.FromImage(backgroudImg);  
    530.   
    531.             //清除画布,背景设置为白色  
    532.             g.Clear(System.Drawing.Color.White);  
    533.   
    534.             int x = 0;  
    535.             for (int i = 0; i < imgs.Count; i++)  
    536.             {  
    537.                 Bitmap bmp = imgs[i];// KiResizeImage(imgs[i], imgs[i].Width, h);  
    538.                 if (bmp == null)  
    539.                     continue;  
    540.                 g.DrawImage(bmp, x, 0, bmp.Width, h);  
    541.                 x = x + bmp.Width + z;  
    542.                 g.Flush();  
    543.             }  
    544.             g.Dispose();  
    545.             return backgroudImg;  
    546.         }  
    547.   
    548.         /// <summary>  
    549.         /// 合并图片  
    550.         /// </summary>  
    551.         /// <param name="imgs">要处理的图片列表</param>  
    552.         /// <param name="z">图片间隔</param>  
    553.         /// <param name="mType">0按高度最高的合并 1 按高度最低的合并</param>  
    554.         /// <returns></returns>  
    555.         public static Bitmap MergerImg(List<Bitmap> imgs, int z, int mType)  
    556.         {  
    557.             if (imgs.Count <= 0)  
    558.                 return null;  
    559.   
    560.             int w = 0;  
    561.             foreach (Bitmap bmp in imgs)  
    562.                 w += bmp.Width;  
    563.             w += z * (imgs.Count - 1);  
    564.             int h = mType == 0 ? GetMaxHeight(imgs) : GetMinHeight(imgs);  
    565.   
    566.             return MergerImg(imgs, z, w, h);  
    567.         }  
    568.   
    569.         /// <summary>  
    570.         /// 缩放  
    571.         /// </summary>  
    572.         /// <param name="bmp">要处理的图片</param>  
    573.         /// <param name="newW">缩放后的宽</param>  
    574.         /// <param name="newH">缩放后的高</param>  
    575.         /// <returns></returns>  
    576.         public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)  
    577.         {  
    578.             if (bmp == null)  
    579.                 return null;  
    580.   
    581.             int max = bmp.Width > bmp.Height ? bmp.Width : bmp.Height;  
    582.             float l = max == bmp.Width ? (float)newW / (float)bmp.Width : (float)newH / (float)bmp.Height;  
    583.             float ww = bmp.Width * l;  
    584.             float hh = bmp.Height * l;  
    585.             ww = ww > 1 ? ww : newW;  
    586.             hh = hh > 1 ? hh : newH;  
    587.             Bitmap b = new Bitmap((int)ww, (int)hh);  
    588.             try  
    589.             {  
    590.                 Graphics g = Graphics.FromImage(b);  
    591.   
    592.                 g.PixelOffsetMode = PixelOffsetMode.Half;  
    593.                 // 插值算法的质量  
    594.                 g.InterpolationMode = InterpolationMode.High;  
    595.                 g.DrawImage(bmp, new Rectangle(0, 0, (int)ww, (int)hh),  
    596.                     new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);  
    597.                 g.Dispose();  
    598.             }  
    599.             catch (Exception ex)  
    600.             {  
    601.                 throw ex;  
    602.             }  
    603.             return b;  
    604.         }  
    605.   
    606.         /// <summary>  
    607.         /// 图片包含的颜色数量  
    608.         /// </summary>  
    609.         /// <param name="img">图片</param>  
    610.         /// <returns></returns>  
    611.         public static List<Color> ColorNumber(Bitmap img)  
    612.         {  
    613.             int w = img.Width, h = img.Height;  
    614.             Color nowColor;  
    615.             //阀值  
    616.             int distance = 90;  
    617.             List<Color> colors = new List<Color>();  
    618.             for (int i = 0; i < w; i++)  
    619.             {  
    620.                 for (int j = 0; j < h; j++)  
    621.                 {  
    622.                     nowColor = img.GetPixel(i, j);  
    623.                     byte r = nowColor.R;  
    624.                     byte g = nowColor.G;  
    625.                     byte b = nowColor.B;  
    626.   
    627.                     if (colors.Count == 0)  
    628.                         colors.Add(nowColor);  
    629.                     else  
    630.                     {  
    631.                         int count = 0;  
    632.                         foreach (Color c in colors)  
    633.                         {  
    634.                             byte or = c.R;  
    635.                             byte og = c.G;  
    636.                             byte ob = c.B;  
    637.                             int R = System.Math.Abs(r - or);  
    638.                             int G = System.Math.Abs(g - og);  
    639.                             int B = System.Math.Abs(b - ob);  
    640.                             double sqr = System.Math.Sqrt(R * R + G * G + B * B);  
    641.                             if (sqr > distance)  
    642.                                 count += 1;  
    643.                         }  
    644.                         if (count >= colors.Count)  
    645.                             colors.Add(nowColor);  
    646.                     }  
    647.                 }  
    648.             }  
    649.             return colors;  
    650.         }  
    651.   
    652.         /// <summary>  
    653.         /// 颜色图片  
    654.         /// </summary>  
    655.         /// <param name="c">颜色</param>  
    656.         /// <param name="width">宽 像素</param>  
    657.         /// <param name="hight">高 像素</param>  
    658.         /// <returns></returns>  
    659.         public static Bitmap GetColorImg(Color c,int width,int height)  
    660.         {  
    661.             Bitmap bmp;  
    662.             if (width > 0 && height > 0)  
    663.             {  
    664.                 bmp = new Bitmap(width, height);  
    665.             }  
    666.             else  
    667.             {  
    668.                 bmp = new Bitmap(10, 10);  
    669.             }  
    670.             for (int i = 0; i < bmp.Width; i++)  
    671.             {  
    672.                 for (int j = 0; j < bmp.Height; j++)  
    673.                 {  
    674.                     bmp.SetPixel(i, j, c);  
    675.                 }  
    676.             }  
    677.             return bmp;  
    678.         }  
    679.   
    680.     }  
    681.   
    682. }  
  • 相关阅读:
    陈欧代言
    location传值
    jsp中button传值
    电影
    排序
    比较两个字符,相等输出yes,不相等输出no
    查表求平方值
    数据库查询调优(SQL 2008)
    HelloWorld
    关于缓存 (这个自己也在慢慢学习,慢慢总结中,有路过的,求指点,赶紧不尽。。。)
  • 原文地址:https://www.cnblogs.com/GmrBrian/p/6980355.html
Copyright © 2011-2022 走看看