using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PaintDotNet.GeneralAnalysis.Artwork { internal class ImageTools { public enum OverlapType { CENTER = 0, //中间重叠 TOPLEFT = 1, //左上角 } /// /// Bitmap裁剪 /// /// 原图 /// 选择区域 /// public static Bitmap Crop(Bitmap src, Rectangle cropRect) { Bitmap target = new Bitmap(cropRect.Width, cropRect.Height); using (Graphics g = Graphics.FromImage(target)) { g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel); } return target; } /// /// 拼合图片 /// /// /// /// /// /// public static Bitmap Overlap(Bitmap[] bitmaps, int width, int height, OverlapType type) { Bitmap bmPhoto = new Bitmap(width, height, PixelFormat.Format32bppArgb); float x = 0; float y = 0; //load the Bitmap into a Graphics object Graphics grPhoto = Graphics.FromImage(bmPhoto); //Set the rendering quality for this Graphics object grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现 //haix for (int i = 0; i < bitmaps.Length; i++) { Bitmap img = bitmaps[i]; if (img == null) { continue; } if (type == OverlapType.CENTER) { x = (width - img.Width) / 2.0f; y = (height - img.Height) / 2.0f; } grPhoto.DrawImage(img, new PointF(x, y)); } return bmPhoto; } /// /// 生成位置导航窗口图像 /// /// /// /// /// /// public static Bitmap CreatView(Bitmap bitmap, int width, int height, PointF pointF) { Bitmap bmPhoto = new Bitmap(width, height); for (int y = 0; y < bmPhoto.Height; y++) { for (int x = 0; x < bmPhoto.Width; x++) { bmPhoto.SetPixel(x, y, Color.FromArgb(0, 0, 0)); } } Graphics grPhoto = Graphics.FromImage(bmPhoto); //Set the rendering quality for this Graphics object grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现 grPhoto.DrawImage(bitmap, pointF); return bmPhoto; } /// /// 任意角度旋转 /// /// 原始图Bitmap /// 旋转角度 /// public static Bitmap RotateFlip(Bitmap bmp, float angle) { int w = bmp.Width + 2; int h = bmp.Height + 2; Bitmap tmp = new Bitmap(w, h); Graphics g = Graphics.FromImage(tmp); g.DrawImageUnscaled(bmp, 1, 1); g.Dispose(); GraphicsPath path = new GraphicsPath(); path.AddRectangle(new RectangleF(0f, 0f, w, h)); Matrix mtrx = new Matrix(); mtrx.Rotate(angle); RectangleF rct = path.GetBounds(mtrx); Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height); g = Graphics.FromImage(dst); g.TranslateTransform(-rct.X, -rct.Y); g.RotateTransform(angle); g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawImageUnscaled(tmp, 0, 0); g.Dispose(); tmp.Dispose(); return dst; } /// /// 调整图片的透明度 /// /// /// /// public static Bitmap MakeTransparent(Bitmap src, int num) { try { int w = src.Width; int h = src.Height; Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); unsafe { byte* pIn = (byte*)srcData.Scan0.ToPointer(); byte* pOut = (byte*)dstData.Scan0.ToPointer(); byte* p; int stride = srcData.Stride; int r, g, b; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { p = pIn; b = pIn[0]; g = pIn[1]; r = pIn[2]; pOut[1] = (byte)g; pOut[2] = (byte)r; pOut[3] = (byte)num; pOut[0] = (byte)b; pIn += 4; pOut += 4; } pIn += srcData.Stride - w * 4; pOut += srcData.Stride - w * 4; } src.UnlockBits(srcData); dstBitmap.UnlockBits(dstData); return dstBitmap; } } catch (Exception) { return null; } } /// /// 两点间距离 /// /// /// /// public static double CalcDistance(Point startPoint, Point endPoint) { int x = System.Math.Abs(endPoint.X - startPoint.X); int y = System.Math.Abs(endPoint.Y - startPoint.Y); return Math.Sqrt(x * x + y * y); } } }