123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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
- {
- /// <summary>
- /// 颗粒图片缩放到固定大小
- /// </summary>
- /// <param name="sourceFile"></param>
- /// <param name="targetWidth"></param>
- /// <param name="targetHeight"></param>
- /// <returns></returns>
- 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;
-
-
- }
- /// <summary>
- /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
- /// </summary>
- /// <param name="in_cotsparticleclr"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// Resize图片
- /// </summary>
- /// <param name="bmp">原始Bitmap</param>
- /// <param name="newW">新的宽度</param>
- /// <param name="newH">新的高度</param>
- /// <returns>处理以后的图片</returns>
- 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;
- }
- }
- }
- }
|