图片处理类 类库--C#
调用如下:
- Bitmap bitmap = new Bitmap("C:\\Users\\Thinkpad\\Desktop\\aa.jpg");
- Bitmap[] bit = new Bitmap[13];
- for (int i = 0; i < 13; i++) {
- bit[i] = new Bitmap("C:\\Users\\Thinkpad\\Desktop\\aa.jpg");
- }
- //去色
- pictureEdit1.Image = ImagePS.DeColor(bit[0]);
- //去色 去掉白色
- pictureEdit2.Image = ImagePS.DeColor(bit[1], Color.White);
- //底片
- pictureEdit3.Image = ImagePS.ReImg(bit[2]);
- //浮雕
- pictureEdit4.Image = ImagePS.Raised(bit[3]);
- //高斯模糊
- pictureEdit5.Image = ImagePS.Softness(bit[4]);
- //锐化
- pictureEdit6.Image = ImagePS.Definition(bit[5]);
- //色相
- pictureEdit7.Image = ImagePS.SetColorFilter(bit[6], ImagePS.ColorFilterType.Red);
- //对比度
- pictureEdit8.Image = ImagePS.Contrast(bit[7], 50);
- //缩放
- pictureEdit9.Image = ImagePS.KiResizeImage(bit[8], 100, 100);
- //亮度调整
- pictureEdit10.Image = ImagePS.LightImage(bit[9], -100);
- //设置曲线
- pictureEdit11.Image = ImagePS.SetGamma(bit[10],250,100,250);
- //得到纯色图片
- pictureEdit12.Image = ImagePS.GetColorImg(Color.Yellow,100,100);
- <strong><span style="font-size:14px;">底层类:</span></strong>
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace Common
- {
- /// <summary>
- /// 图片处理 基础类
- /// </summary>
- public class ImagePS
- {
- public static Bitmap GetControlBmp(Control ctrl, int offsetX, int offsetY)
- {
- Graphics myGraphics = ctrl.CreateGraphics();
- Rectangle r = ctrl.RectangleToScreen(ctrl.Bounds);
- Bitmap memoryImage = new Bitmap(r.Width, r.Height, myGraphics);
- Graphics memoryGraphics = Graphics.FromImage(memoryImage);
- memoryGraphics.CopyFromScreen(r.X - offsetX, r.Y - offsetY, 0, 0, new Size(r.Width, r.Height));
- return memoryImage;
- }
- /// <summary>
- /// 色相类型,红,绿,蓝
- /// </summary>
- public enum ColorFilterType
- {
- Red, Green, Blue
- }
- /// <summary>
- /// 底片效果
- /// </summary>
- /// <param name="img">图片</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap ReImg(Bitmap img)
- {
- byte r, g, b;
- for (int i = 0; i < img.Width; i++)
- {
- for (int j = 0; j < img.Height; j++)
- {
- r = (byte)(255 - img.GetPixel(i, j).R);
- g = (byte)(255 - img.GetPixel(i, j).G);
- b = (byte)(255 - img.GetPixel(i, j).B);
- img.SetPixel(i, j, Color.FromArgb(r, g, b));
- }
- }
- return img;
- }
- /// <summary>
- /// 图片亮度调整
- /// </summary>
- /// <param name="bitmap">图片</param>
- /// <param name="light">亮度[-255,255]</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap LightImage(Bitmap bitmap, int light)
- {
- light = light > 255 ? 255 : light;
- light = light < -255 ? -255 : light;
- int w = bitmap.Width;
- int h = bitmap.Height;
- int pix = 0;
- BitmapData data = bitmap.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
- unsafe
- {
- byte* p = (byte*)data.Scan0;
- int offset = data.Stride - w * 3;
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < h; j++)
- {
- for (int x = 0; x < 3; x++)
- {
- pix = p[x] + light;
- if (light < 0)
- p[x] = (byte)Math.Max(0, pix);
- if (light > 0)
- p[x] = (byte)Math.Min(255, pix);
- }
- p += 3;
- }
- p += offset;
- }
- }
- bitmap.UnlockBits(data);
- return bitmap;
- }
- /// <summary>
- /// 图片去色
- /// </summary>
- /// <param name="img">要去色的图片</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap DeColor(Bitmap img)
- {
- if (img == null)
- return img;
- int w = img.Width, h = img.Height;
- Color oldColor, newColor;
- int y = 0;
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < h; j++)
- {
- oldColor = img.GetPixel(i, j);
- byte r = oldColor.R;
- byte g = oldColor.G;
- byte b = oldColor.B;
- y = (r + g + b) / 3;
- newColor = Color.FromArgb(y, y, y);
- img.SetPixel(i, j, newColor);
- }
- }
- return img;
- }
- /// <summary>
- /// 图片去色
- /// </summary>
- /// <param name="img">要去色的图片</param>
- /// <param name="c">要去掉的颜色</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap DeColor(Bitmap img, Color c)
- {
- int w = img.Width, h = img.Height;
- Color oldColor;
- int cha1 = 60;
- int cha2 = 60;
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < h; j++)
- {
- oldColor = img.GetPixel(i, j);
- byte r = oldColor.R;
- byte g = oldColor.G;
- byte b = oldColor.B;
- bool bol1 = false, bol2 = false, bol3 = false;
- if (r - c.R < cha1 && r - c.R > -cha2)
- bol1 = true;
- if (g - c.G < cha1 && g - c.G > -cha2)
- bol2 = true;
- if (b - c.B < cha1 && b - c.B > -cha2)
- bol3 = true;
- if (bol1 && bol2 && bol3)
- {
- //y = (r + g + b) / 3;
- //newColor = Color.FromArgb(y, y, y);
- img.SetPixel(i, j, Color.Black);
- }
- }
- }
- return img;
- }
- /// <summary>
- /// 浮雕效果
- /// </summary>
- /// <param name="img">要处理的图片</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap Raised(Bitmap img)
- {
- Color pix1, pix2;
- for (int x = 0; x < img.Width - 1; x++)
- {
- for (int y = 0; y < img.Height - 1; y++)
- {
- pix1 = img.GetPixel(x, y);
- pix2 = img.GetPixel(x + 1, y + 1);
- int r = 0, g = 0, b = 0;//新颜色的R,G,B
- r = Math.Abs(pix1.R - pix2.R + 100);
- g = Math.Abs(pix1.G - pix2.G + 100);
- b = Math.Abs(pix1.B - pix2.B + 100);
- //控制上下限 0 -- 255
- r = Math.Min(255, r);
- g = Math.Min(255, g);
- b = Math.Min(255, b);
- r = Math.Max(0, r);
- g = Math.Max(0, g);
- b = Math.Max(0, b);
- img.SetPixel(x, y, Color.FromArgb(r, g, b));
- }
- }
- return img;
- }
- /// <summary>
- /// 高斯模糊
- /// </summary>
- /// <param name="img">要处理的图片</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap Softness(Bitmap img)
- {
- Color pixel;
- int Width = img.Width;
- int Height = img.Height;
- int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
- for (int x = 1; x < Width - 1; x++)
- {
- for (int y = 1; y < Height - 1; y++)
- {
- int r = 0, g = 0, b = 0;
- int Index = 0;
- for (int col = -1; col <= 1; col++)
- {
- for (int row = -1; row <= 1; row++)
- {
- pixel = img.GetPixel(x + row, y + col);
- r += pixel.R * Gauss[Index];
- g += pixel.G * Gauss[Index];
- b += pixel.B * Gauss[Index];
- Index++;
- }
- }
- r /= 16;
- g /= 16;
- b /= 16;
- //处理颜色值溢出
- r = r > 255 ? 255 : r;
- r = r < 0 ? 0 : r;
- g = g > 255 ? 255 : g;
- g = g < 0 ? 0 : g;
- b = b > 255 ? 255 : b;
- b = b < 0 ? 0 : b;
- img.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
- }
- }
- return img;
- }
- /// <summary>
- /// 对比度
- /// </summary>
- /// <param name="img">要处理的图片</param>
- /// <param name="m">对比度[-100,100]</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap Contrast(Bitmap img, int m)
- {
- if (m < -100) m = -100;
- if (m > 100) m = 100;
- //Width, Height
- int w = img.Width;
- int h = img.Height;
- double pix = 0;
- BitmapData data = img.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
- int offset = data.Stride - 3 * w;
- double contrast = (100.00 + m) / 100.00;
- contrast *= contrast;
- unsafe
- {
- byte* p = (byte*)data.Scan0;
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < h; j++)
- {
- for (int y = 0; y < 3; y++)
- {
- pix = ((p[y] / 255.00 - 0.5) * contrast + 0.5) * 255;
- if (pix < 0) pix = 0;
- if (pix > 255) pix = 255;
- p[y] = (byte)pix;
- }
- p += 3;
- }
- p += offset;
- }
- }
- img.UnlockBits(data);
- return img;
- }
- /// <summary>
- /// 锐化
- /// </summary>
- /// <param name="img">要处理的图片</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap Definition(Bitmap img)
- {
- int w = img.Width;
- int h = img.Height;
- Color c;
- int[] laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
- for (int i = 1; i < w - 1; i++)
- {
- for (int j = 1; j < h - 1; j++)
- {
- int r = 0, g = 0, b = 0;
- int index = 0;
- for (int col = -1; col <= 1; col++)
- {
- for (int row = -1; row <= 1; row++)
- {
- c = img.GetPixel(i + row, j + col);
- r += c.R * laplacian[index];
- g += c.G * laplacian[index];
- b += c.B * laplacian[index];
- index += 1;
- }
- }
- r = Math.Max(0, r);
- r = Math.Min(255, r);
- g = Math.Max(0, g);
- g = Math.Min(255, g);
- b = Math.Max(0, b);
- b = Math.Min(255, b);
- img.SetPixel(i - 1, j - 1, Color.FromArgb(r, g, b));
- }//end for
- }//end for
- return img;
- }
- /// <summary>
- /// 雾化
- /// </summary>
- /// <param name="img">要处理的图片</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap Atomization(Bitmap img)
- {
- int w = img.Width;
- int h = img.Height;
- Color pix;
- //在此实例化,雾化效果
- Random ran = new Random();
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- //Random ran = new Random(); 在此实例化,水一样的效果
- int dx = x + ran.Next(1234567) % 17;
- int dy = y + ran.Next(1234567) % 17;
- pix = img.GetPixel(Math.Min(dx, w - 1), Math.Min(dy, h - 1));
- img.SetPixel(x, y, pix);
- }
- }
- return img;
- }
- /// <summary>
- /// 设置色相
- /// </summary>
- /// <param name="img">要处理的图片</param>
- /// <param name="cType">色相</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap SetColorFilter(Bitmap img, ColorFilterType cType)
- {
- Color c;
- int r = 0, g = 0, b = 0;
- for (int x = 0; x < img.Width; x++)
- {
- for (int y = 0; y < img.Height; y++)
- {
- c = img.GetPixel(x, y);
- r = c.R;
- g = c.G;
- b = c.B;
- if (cType == ColorFilterType.Red)
- {
- g -= 255;
- b -= 255;
- }
- else if (cType == ColorFilterType.Green)
- {
- r -= 255;
- b -= 255;
- }
- else
- {
- r -= 255;
- g -= 255;
- }
- //控制色值大小 0 -- 255
- r = Math.Max(0, r);
- g = Math.Max(0, g);
- b = Math.Max(0, b);
- r = Math.Min(255, r);
- g = Math.Min(255, g);
- b = Math.Min(255, b);
- img.SetPixel(x, y, Color.FromArgb(r, g, b));
- }
- }
- return img;
- }
- /// <summary>
- /// 曲线
- /// </summary>
- /// <param name="img">要处理的图片</param>
- /// <param name="red">红</param>
- /// <param name="green">绿</param>
- /// <param name="blue">蓝</param>
- /// <returns>处理后的图片</returns>
- public static Bitmap SetGamma(Bitmap img, byte red, byte green, byte blue)
- {
- Color c;
- byte[] reds = CreateGamma(red);
- byte[] greens = CreateGamma(green);
- byte[] blues = CreateGamma(blue);
- for (int x = 0; x < img.Width; x++)
- {
- for (int y = 0; y < img.Height; y++)
- {
- c = img.GetPixel(x, y);
- img.SetPixel(x, y, Color.FromArgb(reds[c.R], greens[c.G], blues[c.B]));
- }
- }
- return img;
- }
- //创建 曲线数组
- private static byte[] CreateGamma(byte color)
- {
- byte[] gammas = new byte[256];
- for (int i = 0; i < 256; i++)
- {
- gammas[i] = Math.Min((byte)255, (byte)(255.0F * Math.Pow(i / 255, 1.0F / color) + 0.5F));
- }
- return gammas;
- }
- /// <summary>
- /// 合并图片
- /// </summary>
- /// <param name="imgs">要处理的图片列表</param>
- /// <param name="z">图片间隔</param>
- /// <returns></returns>
- public static Bitmap MergerImg(List<Bitmap> imgs, int z)
- {
- if (imgs.Count <= 0)
- return null;
- int w = 0;
- foreach (Bitmap bmp in imgs)
- if (bmp != null)
- w = w + bmp.Width;
- w += z * (imgs.Count - 1);
- int h = GetMaxHeight(imgs);
- return MergerImg(imgs, z, w, h);
- }
- private static int GetMaxHeight(List<Bitmap> imgs)
- {
- if (imgs == null || imgs.Count == 0)
- return 0;
- int maxHeight = 0;
- foreach (Bitmap bmp in imgs)
- {
- if (bmp == null)
- continue;
- if (maxHeight == -1)
- {
- maxHeight = bmp.Height;
- }
- else
- maxHeight = bmp.Height > maxHeight ? bmp.Height : maxHeight;
- }
- return maxHeight;
- }
- private static int GetMinHeight(List<Bitmap> imgs)
- {
- if (imgs == null || imgs.Count == 0)
- return 0;
- int mixHeight = 0;
- foreach (Bitmap bmp in imgs)
- {
- if (mixHeight == 0)
- {
- mixHeight = bmp.Height;
- }
- else
- mixHeight = bmp.Height < mixHeight ? bmp.Height : mixHeight;
- }
- return mixHeight;
- }
- private static Bitmap MergerImg(List<Bitmap> imgs, int z, int w, int h)
- {
- //创建要显示的图片对象,根据参数的个数设置宽度
- Bitmap backgroudImg = new Bitmap(w, h);
- Graphics g = Graphics.FromImage(backgroudImg);
- //清除画布,背景设置为白色
- g.Clear(System.Drawing.Color.White);
- int x = 0;
- for (int i = 0; i < imgs.Count; i++)
- {
- Bitmap bmp = imgs[i];// KiResizeImage(imgs[i], imgs[i].Width, h);
- if (bmp == null)
- continue;
- g.DrawImage(bmp, x, 0, bmp.Width, h);
- x = x + bmp.Width + z;
- g.Flush();
- }
- g.Dispose();
- return backgroudImg;
- }
- /// <summary>
- /// 合并图片
- /// </summary>
- /// <param name="imgs">要处理的图片列表</param>
- /// <param name="z">图片间隔</param>
- /// <param name="mType">0按高度最高的合并 1 按高度最低的合并</param>
- /// <returns></returns>
- public static Bitmap MergerImg(List<Bitmap> imgs, int z, int mType)
- {
- if (imgs.Count <= 0)
- return null;
- int w = 0;
- foreach (Bitmap bmp in imgs)
- w += bmp.Width;
- w += z * (imgs.Count - 1);
- int h = mType == 0 ? GetMaxHeight(imgs) : GetMinHeight(imgs);
- return MergerImg(imgs, z, w, h);
- }
- /// <summary>
- /// 缩放
- /// </summary>
- /// <param name="bmp">要处理的图片</param>
- /// <param name="newW">缩放后的宽</param>
- /// <param name="newH">缩放后的高</param>
- /// <returns></returns>
- public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
- {
- if (bmp == null)
- return null;
- int max = bmp.Width > bmp.Height ? bmp.Width : bmp.Height;
- float l = max == bmp.Width ? (float)newW / (float)bmp.Width : (float)newH / (float)bmp.Height;
- float ww = bmp.Width * l;
- float hh = bmp.Height * l;
- ww = ww > 1 ? ww : newW;
- hh = hh > 1 ? hh : newH;
- Bitmap b = new Bitmap((int)ww, (int)hh);
- try
- {
- Graphics g = Graphics.FromImage(b);
- g.PixelOffsetMode = PixelOffsetMode.Half;
- // 插值算法的质量
- g.InterpolationMode = InterpolationMode.High;
- g.DrawImage(bmp, new Rectangle(0, 0, (int)ww, (int)hh),
- new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
- g.Dispose();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return b;
- }
- /// <summary>
- /// 图片包含的颜色数量
- /// </summary>
- /// <param name="img">图片</param>
- /// <returns></returns>
- public static List<Color> ColorNumber(Bitmap img)
- {
- int w = img.Width, h = img.Height;
- Color nowColor;
- //阀值
- int distance = 90;
- List<Color> colors = new List<Color>();
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < h; j++)
- {
- nowColor = img.GetPixel(i, j);
- byte r = nowColor.R;
- byte g = nowColor.G;
- byte b = nowColor.B;
- if (colors.Count == 0)
- colors.Add(nowColor);
- else
- {
- int count = 0;
- foreach (Color c in colors)
- {
- byte or = c.R;
- byte og = c.G;
- byte ob = c.B;
- int R = System.Math.Abs(r - or);
- int G = System.Math.Abs(g - og);
- int B = System.Math.Abs(b - ob);
- double sqr = System.Math.Sqrt(R * R + G * G + B * B);
- if (sqr > distance)
- count += 1;
- }
- if (count >= colors.Count)
- colors.Add(nowColor);
- }
- }
- }
- return colors;
- }
- /// <summary>
- /// 颜色图片
- /// </summary>
- /// <param name="c">颜色</param>
- /// <param name="width">宽 像素</param>
- /// <param name="hight">高 像素</param>
- /// <returns></returns>
- public static Bitmap GetColorImg(Color c,int width,int height)
- {
- Bitmap bmp;
- if (width > 0 && height > 0)
- {
- bmp = new Bitmap(width, height);
- }
- else
- {
- bmp = new Bitmap(10, 10);
- }
- for (int i = 0; i < bmp.Width; i++)
- {
- for (int j = 0; j < bmp.Height; j++)
- {
- bmp.SetPixel(i, j, c);
- }
- }
- return bmp;
- }
- }
- }