123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- 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, //左上角
- }
- /// <summary>
- /// Bitmap裁剪
- /// </summary>
- /// <param name="src">原图</param>
- /// <param name="cropRect">选择区域</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 拼合图片
- /// </summary>
- /// <param name="bitmaps"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 生成位置导航窗口图像
- /// </summary>
- /// <param name="bitmap"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="pointF"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 任意角度旋转
- /// </summary>
- /// <param name="bmp">原始图Bitmap</param>
- /// <param name="angle">旋转角度</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 调整图片的透明度
- /// </summary>
- /// <param name="src"></param>
- /// <param name="num"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 两点间距离
- /// </summary>
- /// <param name="startPoint"></param>
- /// <param name="endPoint"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
|