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
{
///
/// 绘制标尺
///
public class DrawRulerHelper
{
///
/// 缩放图像
///
///
///
///
///
///
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;
}
///
/// 深拷贝,反射
///
///
///
public static T DeepCopyByReflect(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 DeepCopyListByReflect(List objs)
{
List list = new List();
if (objs != null && objs.Count > 0)
{
foreach (T t in objs)
{
list.Add(DeepCopyByReflect(t));
}
}
return list;
}
///
/// base64转bitmap
///
///
///
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;
}
}
///
/// bitmap转base64
///
///
///
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;
}
}
}