123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using SmartCoalApplication.Base.Enum;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace SmartCoalApplication.Base.CommTool
- {
- /// <summary>
- /// 绘制标尺
- /// </summary>
- public class DrawRulerHelper
- {
- /// <summary>
- /// 缩放图像
- /// </summary>
- /// <param name="originalImage"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="mode"></param>
- /// <returns></returns>
- public static Image ResizeImage(Image originalImage, int width, int height, ThumbnailMode mode)
- {
- int towidth = width;
- int toheight = height;
- int x = 0;
- int y = 0;
- int initWidth = originalImage.Width;
- int initHeight = originalImage.Height;
- switch (mode)
- {
- case ThumbnailMode.UsrHeightWidth: //指定高宽缩放(可能变形)
- break;
- case ThumbnailMode.UsrHeightWidthBound: //指定高宽缩放(可能变形)(过小则不变)
- if (originalImage.Width <= width && originalImage.Height <= height)
- {
- return originalImage;
- }
- if (originalImage.Width < width)
- {
- towidth = originalImage.Width;
- }
- if (originalImage.Height < height)
- {
- toheight = originalImage.Height;
- }
- break;
- case ThumbnailMode.UsrWidth: //指定宽,高按比例
- toheight = originalImage.Height * width / originalImage.Width;
- break;
- case ThumbnailMode.UsrWidthBound: //指定宽(过小则不变),高按比例
- if (originalImage.Width <= width)
- {
- return originalImage;
- }
- else
- {
- toheight = originalImage.Height * width / originalImage.Width;
- }
- break;
- case ThumbnailMode.UsrHeight: //指定高,宽按比例
- towidth = originalImage.Width * height / originalImage.Height;
- break;
- case ThumbnailMode.UsrHeightBound: //指定高(过小则不变),宽按比例
- if (originalImage.Height <= height)
- {
- return originalImage;
- }
- else
- {
- towidth = originalImage.Width * height / originalImage.Height;
- }
- break;
- case ThumbnailMode.Cut: //指定高宽裁减(不变形)
- //计算宽高比
- double srcScale = (double)originalImage.Width / (double)originalImage.Height;
- double destScale = (double)towidth / (double)toheight;
- //宽高比相同
- if (srcScale - destScale >= 0 && srcScale - destScale <= 0.001)
- {
- x = 0;
- y = 0;
- initWidth = originalImage.Width;
- initHeight = originalImage.Height;
- }
- //源宽高比大于目标宽高比
- //(源的宽比目标的宽大)
- else if (srcScale > destScale)
- {
- initWidth = originalImage.Height * towidth / toheight;
- initHeight = originalImage.Height;
- x = (originalImage.Width - initWidth) / 2;
- y = 0;
- }
- //源宽高比小于目标宽高小,源的高度大于目标的高度
- else
- {
- initWidth = originalImage.Width;
- initHeight = originalImage.Width * height / towidth;
- x = 0;
- y = (originalImage.Height - initHeight) / 2;
- }
- break;
- default:
- break;
- }
- Image bitmap = new Bitmap(towidth, toheight);
- //新建一个画板
- using (Graphics g = Graphics.FromImage(bitmap))
- {
- //设置高质量插值法
- g.CompositingQuality = CompositingQuality.HighQuality;
- //设置高质量,低速段呈现的平滑程度
- g.SmoothingMode = SmoothingMode.HighQuality;
- //在指定的位置上,并按指定大小绘制原图片的指定部分
- g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, initWidth, initHeight), GraphicsUnit.Pixel);
- }
- return bitmap;
- }
- /// <summary>
- /// 深拷贝,反射
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static T DeepCopyByReflect<T>(T obj)
- {
- //如果是字符串或值类型则直接返回
- if (obj == null || obj is string || obj.GetType().IsValueType) return obj;
- object retval = Activator.CreateInstance(obj.GetType());
- FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
- foreach (FieldInfo field in fields)
- {
- try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); }
- catch { }
- }
- return (T)retval;
- }
- public static List<T> DeepCopyListByReflect<T>(List<T> objs)
- {
- List<T> list = new List<T>();
- if (objs != null && objs.Count > 0)
- {
- foreach (T t in objs)
- {
- list.Add(DeepCopyByReflect(t));
- }
- }
- return list;
- }
- /// <summary>
- /// base64转bitmap
- /// </summary>
- /// <param name="strbase64"></param>
- /// <returns></returns>
- public static Bitmap Base64StringToImage(string strbase64)
- {
- try
- {
- byte[] arr = Convert.FromBase64String(strbase64);
- MemoryStream ms = new MemoryStream(arr);
- Bitmap bmp = new Bitmap(ms);
- ms.Close();
- return bmp;
- }
- catch (Exception)
- {
- return null;
- }
- }
- /// <summary>
- /// bitmap转base64
- /// </summary>
- /// <param name="bmp"></param>
- /// <returns></returns>
- public static string ImgToBase64String(Bitmap source)
- {
- OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(source);
- Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
- string base64 = "";
- using (MemoryStream ms = new MemoryStream())
- {
- bitmap.Save(ms, ImageFormat.Jpeg);
- base64 = Convert.ToBase64String(ms.ToArray());
- ms.Close();
- bitmap.Dispose();
- }
- return base64;
- }
- }
- }
|