using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; namespace AllBase { public class ImageBuild { public byte[] Img; public int Width = 0, Height = 0; public Image InitImage = null; public ImageBuild(byte[] image) { if (image == null || image.Length == 0) return; Img = image; try { MemoryStream ms = new MemoryStream(Img); InitImage = Image.FromStream(ms); ms.Close(); } catch (Exception e) { LogSave.Add(e.Message); } Width = InitImage.Width; Height = InitImage.Height; } /// /// 裁剪为指定大小,不符合则返回null /// /// /// /// public byte[] CutToSize(int width, int height) { if (InitImage == null) return null; int w = InitImage.Width; int h = InitImage.Height; if (w < width || h < height) { //return null; width = w; height = h; } int x = 0, y = 0, nw = w, nh = h; int tx = 0, ty = 0, tw = width, th = height; double f = w * 1.0 / width; double t = h * 1.0 / height; if (f == t)//无需变化 { } else if (f > t)//宽度较大 { x = (int)(((f - t) / 2) * width); nw = w - x * 2; } else//高度较大 { y = (int)((t - f) / 2 * height); nh = h - y * 2; } Rectangle fromR = new Rectangle(x, y, nw, nh); Rectangle toR = new Rectangle(tx, ty, tw, th); Bitmap pickedImage = new Bitmap(width, height); Graphics pickedG = Graphics.FromImage(pickedImage); pickedG.InterpolationMode = InterpolationMode.Low; pickedG.SmoothingMode = SmoothingMode.HighSpeed; pickedG.DrawImage(InitImage, toR, fromR, GraphicsUnit.Pixel); //转图片后转二进制数组 Image initImagex = (Image)pickedImage.Clone(); MemoryStream ms = new MemoryStream(); ImageCodecInfo icc = GetCodeInfo("1.jpg"); long quality = 75; EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(Encoder.Quality, quality); initImagex.Save(ms, icc, ep); byte[] bytes = ms.GetBuffer(); return bytes; } /// /// 添加线条 /// /// public void AddLineToImage(ref byte[] img) { MemoryStream ms = new MemoryStream(img); var image = (Bitmap)Image.FromStream(ms); ms.Close(); int width = image.Width; int height = image.Height; if (width < 100 || height < 100) return; int r = Randomx.GetId(100, 256); int b = Randomx.GetId(100, 256); int g = Randomx.GetId(100, 256); int h = Randomx.GetId(1, 10); var c = Color.FromArgb(r, g, b); for (int i = 0; i < h; i++) { for (int j = 0; j < height; j++) image.SetPixel(i, j, c); for (int j = 0; j < width; j++) image.SetPixel(j, i, c); } for (int i = height - h; i < height; i++) { for (int j = 0; j < width; j++) image.SetPixel(j, i, c); } for (int i = width - h; i < width; i++) { for (int j = 0; j < height; j++) image.SetPixel(i, j, c); } ImageCodecInfo icc = GetCodeInfo("1.jpg"); long quality = 75; ms = new MemoryStream(); EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(Encoder.Quality, quality); image.Save(ms, icc, ep); img = ms.ToArray(); //FileBase.WriteBytes("1.jpg", bytes); } /// /// 输出固定大小图片,背景白色填充,返回JPG格式二进制代码 /// /// /// /// 最大尺寸KB /// public byte[] BuildNewSize(int width, int height, int maxSize) { if (InitImage == null) return null; int w = InitImage.Width; int h = InitImage.Height; int x = 0, y = 0, nw = w, nh = h; int tx = 0, ty = 0, tw = width, th = height; //先缩放 if (w <= width && h <= height)//无需缩放 { tx = (width - w) / 2; tw = w; ty = (height - h) / 2; th = h; } else if (w < width && h > height) { tx = (width - w) / 2; tw = w; y = (h - height) / 2; nh = height; } else if (w > width && h < height) { ty = (height - h) / 2; th = h; x = (w - width) / 2; nw = width; } else { double f = w * 1.0 / width; double t = h * 1.0 / height; if (f == t)//无需变化 { } else if (f > t)//宽度较大 { x = (int)(((f - t) / 2) * width); nw = w - x * 2; } else//高度较大 { y = (int)((t - f) / 2 * height); nh = h - y * 2; } } Rectangle fromR = new Rectangle(x, y, nw, nh); Rectangle toR = new Rectangle(tx, ty, tw, th); Bitmap pickedImage = new Bitmap(width, height); for (int i = 0; i < pickedImage.Width; i++) { for (int j = 0; j < pickedImage.Height; j++) { Color color = Color.FromArgb(255, 255, 255); pickedImage.SetPixel(i, j, color); } } Graphics pickedG = Graphics.FromImage(pickedImage); pickedG.InterpolationMode = InterpolationMode.Low; pickedG.SmoothingMode = SmoothingMode.HighSpeed; pickedG.DrawImage(InitImage, toR, fromR, GraphicsUnit.Pixel); //转图片后转二进制数组 Image initImagex = (Image)pickedImage.Clone(); MemoryStream ms = new MemoryStream(); ImageCodecInfo icc = GetCodeInfo("1.jpg"); long quality = 75; EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); initImagex.Save(ms, icc, ep); byte[] bytes = ms.GetBuffer(); if (bytes.Length > maxSize * 1024) { ms = new MemoryStream(); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 65l); initImagex.Save(ms, icc, ep); bytes = ms.GetBuffer(); } if (bytes.Length > maxSize * 1024) { ms = new MemoryStream(); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 55l); initImagex.Save(ms, icc, ep); bytes = ms.GetBuffer(); } if (bytes.Length > maxSize * 1024) { return null; } //ImageCodecInfo icc = GetCodeInfo("1.jpg"); //initImagex.Save("22.jpg", icc, ep); return bytes; } /// /// 根据路劲得出图片编码信息 /// /// /// public ImageCodecInfo GetCodeInfo(string path) { string exe = path.Split('.')[path.Split('.').Length - 1].ToLower(); ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null, defCodecInfo = null; foreach (ImageCodecInfo i in icis) { if (i.MimeType == "image/jpeg") defCodecInfo = i; if ((exe == "jpg" || exe == "jpeg") && i.MimeType == "image/jpeg") { ici = i; } if (exe == "bmp" && i.MimeType == "image/bmp") { ici = i; } if (exe == "png" && i.MimeType == "image/png") { ici = i; } if (exe == "gif" && i.MimeType == "image/gif") { ici = i; } } if (ici == null) ici = defCodecInfo; return ici; } /// /// image 大小转换 /// /// /// /// public static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size) { //获取图片宽度 int sourceWidth = imgToResize.Width; //获取图片高度 int sourceHeight = imgToResize.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; //计算宽度的缩放比例 nPercentW = ((float)size.Width / (float)sourceWidth); //计算高度的缩放比例 nPercentH = ((float)size.Height / (float)sourceHeight); if (nPercentH < nPercentW) nPercent = nPercentH; else nPercent = nPercentW; //期望的宽度 int destWidth = (int)(sourceWidth * nPercent); //期望的高度 int destHeight = (int)(sourceHeight * nPercent); Bitmap b = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage((System.Drawing.Image)b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; //绘制图像 g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); g.Dispose(); return (System.Drawing.Image)b; } [DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject); /// /// 将Image转换为Ico /// /// /// /// public static Icon ConvertToIcon(Image image) { Bitmap bmp = new Bitmap(image); IntPtr h = bmp.GetHicon(); Icon icon = Icon.FromHandle(h); DeleteObject(h); return icon; } } }