using OpenCvSharp; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OTSIncAReportApp._1_UI.OTSReportExport.DataIntegration { class ImageProcessor { /// /// 颗粒图片缩放到固定大小 /// /// /// /// /// public Bitmap ResizeImageWithPadding(Bitmap sourceFile, int targetWidth, int targetHeight, Color color) { var originalImage = sourceFile; // 计算缩放后的尺寸,保持宽高比 int scaledWidth, scaledHeight; if (originalImage.Width > originalImage.Height) { scaledWidth = targetWidth; scaledHeight = (int)(targetWidth * ((double)originalImage.Height / originalImage.Width)); } else { scaledHeight = targetHeight; scaledWidth = (int)(targetHeight * ((double)originalImage.Width / originalImage.Height)); } // 创建新的Bitmap,大小为目标尺寸 Bitmap newImage = new Bitmap(targetWidth, targetHeight); Graphics graphic = Graphics.FromImage(newImage); // 填充整个图像为黑色 graphic.Clear(color); // 设置高质量插值法 //graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // 计算图片在新图像中的位置(居中) int x = (targetWidth - scaledWidth) / 2; int y = (targetHeight - scaledHeight) / 2; // 绘制缩放后的图片 graphic.DrawImage(originalImage, new Rectangle(x, y, scaledWidth, scaledHeight)); // 保存新图像 //newImage.Save(destFile, ImageFormat.Png); // 或使用ImageFormat.Jpeg等 return newImage; } /// /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒 /// /// /// public Bitmap GetBitmapByParticle(Bitmap ls_bt, Rectangle offset_rect) { //为了能把整个颗粒显示完整 offset_rect.X = offset_rect.X - 5; offset_rect.Y = offset_rect.Y - 5; offset_rect.Width = offset_rect.Width + 10; offset_rect.Height = offset_rect.Height + 10; //防止计算偏差后,有坐标溢出现象 if (offset_rect.X < 0) offset_rect.X = 0; if (offset_rect.Y < 0) offset_rect.Y = 0; if (offset_rect.X + offset_rect.Width > ls_bt.Width) { offset_rect.Width = ls_bt.Width - offset_rect.X; } if (offset_rect.Y + offset_rect.Height > ls_bt.Height) { offset_rect.Height = ls_bt.Height - offset_rect.Y; } Bitmap new_ret_bp; //防止为0后面计算出错 if (offset_rect.Width > 0 && offset_rect.Height > 0) { //最后通过list_showsegment组建成新的图片,进行返回 new_ret_bp = ls_bt.Clone(offset_rect, ls_bt.PixelFormat); } else { new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height); } return new_ret_bp; } public Bitmap GetReZoomBitmap(Bitmap in_bp) { Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(in_bp); Mat dst = new Mat(); Cv2.AdaptiveThreshold(src, dst, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 27, 25); //绝对缩放, Mat dst2 = new Mat(); int col = dst.Width;//获取原图像的大小 int rows = dst.Height; Cv2.Resize(dst, dst2, new OpenCvSharp.Size(4 * col, 4 * rows), 0, 0, InterpolationFlags.Cubic); return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(dst2); } /// /// Resize图片 /// /// 原始Bitmap /// 新的宽度 /// 新的高度 /// 处理以后的图片 public Bitmap KiResizeImage(Bitmap bmp, int newW, int newH) { try { Bitmap b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b);                 // 插值算法的质量                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch { return null; } } } }