using OTSCommon.DBOperate.Model; using OTSIncAReportGraph.Class; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace OTSIncAReportApp._3_ServiceCenter.DataOperation.DataAccess { public class FieldImage { string imagePath; public FieldImage(string rstpath) { imagePath= rstpath + @"\FIELD_FILES\"; } /// /// 拼接颗粒黑白图 /// /// 拼接字符串 /// xs = pixw / ScanFieldSize /// 报告文件路径 /// 图像高 /// 帧图宽 /// KEY为FidANDpID /// public Bitmap GetBlackAndWhiteBitmapForMergedParticle(string sub, double xs, int picHeight, int picWidth,DataTable PartdataTable,Dictionary> segsData) { string vs = "," + sub.Replace(':', '-') + ","; if (PartdataTable.Rows.Count == 0) { return null; } //内接矩形 double max_Y = -1; double max_X = -1; double min_Y = 99999999; double min_X = 99999999; //拼接field矩形 double B_Y = Convert.ToInt64(PartdataTable.Rows[0]["FieldPosY"]) * xs; double R_X = Convert.ToInt64(PartdataTable.Rows[0]["FieldPosX"]) * xs; double T_Y = B_Y; double L_X = R_X; foreach (DataRow item in PartdataTable.Rows) { double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs; if (lefttopXH > R_X) { R_X = lefttopXH; } if (lefttopXH < L_X) { L_X = lefttopXH; } double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs; if (lrfttopYH < B_Y) { B_Y = lrfttopYH; } if (lrfttopYH > T_Y) { T_Y = lrfttopYH; } } int WIDTH = Math.Abs(Convert.ToInt32(R_X - L_X)) + picWidth; int HEIGHT = Math.Abs(Convert.ToInt32(B_Y - T_Y)) + picHeight; //构造最终的图片白板 Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT); Graphics graph = Graphics.FromImage(tableChartImage); graph.Clear(Color.White); graph.DrawImage(tableChartImage, 0, 0); foreach (DataRow item in PartdataTable.Rows) { int x = Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - L_X)); int y = Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - T_Y)); //颗粒外接矩形 double lefttopX = x + Convert.ToInt64(item["RectLeft"]); if (lefttopX < min_X) { min_X = lefttopX; } if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X) { max_X = lefttopX + Convert.ToInt64(item["RectWidth"]); } double lrfttopY = y + Convert.ToInt64(item["RectTop"]); if (lrfttopY + Convert.ToInt64(item["RectHeight"]) > max_Y) { max_Y = lrfttopY + Convert.ToInt64(item["RectHeight"]); } if (lrfttopY < min_Y) { min_Y = lrfttopY; } string key = item["fieldid"].ToString() + "_" + item["particleid"].ToString(); foreach (Segment seg in segsData[key]) { int f_length = seg.Length; for (int m = 0; m < f_length; m++) { int lsjs_x = seg.Start + m + x; int lsjs_y = seg.Height + y; tableChartImage.SetPixel(lsjs_x, lsjs_y, Color.Black); } } } int width = Convert.ToInt32(max_X - min_X); int height = Convert.ToInt32(max_Y - min_Y); int X = Convert.ToInt32(min_X); int Y = Convert.ToInt32(min_Y); Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height }; Bitmap bmap = CapturePic(tableChartImage, rectangle); return bmap; } public Bitmap ReadImageFile(string filename) { if (!File.Exists(imagePath+filename)) { return null;//文件不存在 } FileStream fs = File.OpenRead(imagePath + filename); //OpenRead int filelength = 0; filelength = (int)fs.Length; //获得文件长度 Byte[] image = new Byte[filelength]; //建立一个字节数组 fs.Read(image, 0, filelength); //按字节流读取 System.Drawing.Image result = System.Drawing.Image.FromStream(fs); fs.Close(); Bitmap bit = new Bitmap(result); return bit; } /// /// 拼接颗粒图 /// /// 拼接字符串 /// xs = pixw / ScanFieldSize /// 报告文件路径 /// 图像高 /// 帧图宽 /// public Bitmap GetBitmapForMergedParticle(string sub, double xs, int picHeight, int picWidth,DataTable dataTable) { string vs = "," + sub.Replace(':', '-') + ","; if (dataTable.Rows.Count == 0) { return null; } //内接矩形 double max_Y = -1; double max_X = -1; double min_Y = 99999999; double min_X = 99999999; //拼接field矩形 double B_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs; double R_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs; double T_Y = B_Y; double L_X = R_X; foreach (DataRow item in dataTable.Rows) { double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs; if (lefttopXH > R_X) { R_X = lefttopXH; } if (lefttopXH < L_X) { L_X = lefttopXH; } double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs; if (lrfttopYH < B_Y) { B_Y = lrfttopYH; } if (lrfttopYH > T_Y) { T_Y = lrfttopYH; } } int WIDTH = Math.Abs(Convert.ToInt32(R_X - L_X)) + picWidth; int HEIGHT = Math.Abs(Convert.ToInt32(B_Y - T_Y)) + picHeight; //构造最终的图片白板 Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT); Graphics graph = Graphics.FromImage(tableChartImage); graph.DrawImage(tableChartImage, 0, 0); foreach (DataRow item in dataTable.Rows) { string path1= "Field" + item["fieldid"].ToString() + ".bmp"; //然后将取出的数据,转换成Bitmap对象 Bitmap ls_bt = ReadImageFile(path1); int x = Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - L_X)); int y = Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - T_Y)); graph.DrawImage(ls_bt, x, y); //颗粒外接矩形 double lefttopX = x + Convert.ToInt64(item["RectLeft"]); if (lefttopX < min_X) { min_X = lefttopX; } if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X) { max_X = lefttopX + Convert.ToInt64(item["RectWidth"]); } double lrfttopY = y + Convert.ToInt64(item["RectTop"]); if (lrfttopY + Convert.ToInt64(item["RectHeight"]) > max_Y) { max_Y = lrfttopY + Convert.ToInt64(item["RectHeight"]); } if (lrfttopY < min_Y) { min_Y = lrfttopY; } } int width = Convert.ToInt32(max_X - min_X); int height = Convert.ToInt32(max_Y - min_Y); int X = Convert.ToInt32(min_X); int Y = Convert.ToInt32(min_Y); Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height }; Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed); graph.DrawImage(tableChartImage, 0, 0); return bmap; } /// /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒 /// /// /// public Bitmap GetBitmapByParticle(DataTable dataTable) { string path1 = "Field" + dataTable.Rows[0]["fieldid"].ToString() + ".bmp"; //然后将取出的数据,转换成Bitmap对象 Bitmap ls_bt = ReadImageFile(path1); Rectangle offset_rect = new Rectangle() { X = Convert.ToInt32(dataTable.Rows[0]["RectLeft"]), Y = Convert.ToInt32(dataTable.Rows[0]["RectTop"]), Width = Convert.ToInt32(dataTable.Rows[0]["RectWidth"]), Height = Convert.ToInt32(dataTable.Rows[0]["RectHeight"]) }; //为了能把整个颗粒显示完整 offset_rect.X = offset_rect.X - 20; offset_rect.Y = offset_rect.Y - 20; offset_rect.Width = offset_rect.Width + 40; offset_rect.Height = offset_rect.Height + 40; //防止计算偏差后,有坐标溢出现象 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; } /// /// 获取全部颗粒图 /// /// 拼接颗粒连接字符串 /// 颗粒表 /// pixel /// 帧图高 /// 帧图宽 /// public Bitmap GetBitmapForParticleAll(string sub, DataTable dataTable, double xs=0, int picHeight=0, int picWidth=0) { Bitmap bmap; if(sub != "") { bmap = GetBitmapForMergedParticle(sub, xs, picHeight, picWidth,dataTable); } else { bmap=GetBitmapByParticle(dataTable); } return bmap; } /// /// 获取全部颗粒图 /// /// 拼接颗粒连接字符串 /// 颗粒表 /// pixel /// 帧图高 /// 帧图宽 /// public Bitmap GetBitmapForParticleAllWithBlackAndWhite(string sub, DataTable dataTable, Dictionary> segsData, double xs = 0, int picHeight = 0, int picWidth = 0) { Bitmap bmap; if (sub != "") { bmap = GetBlackAndWhiteBitmapForMergedParticle(sub, xs, picHeight, picWidth, dataTable, segsData); } else { List liseg=segsData[dataTable.Rows[0]["fieldid"].ToString() + "_" + dataTable.Rows[0]["particleid"].ToString()]; bmap = GetBlackAndWhiteImage(dataTable, picHeight, picWidth,liseg); } return bmap; } public Bitmap CapturePic(Bitmap ls_bt, Rectangle offset_rect) { //为了能把整个颗粒显示完整 offset_rect.X = offset_rect.X - 20; offset_rect.Y = offset_rect.Y - 20; offset_rect.Width = offset_rect.Width + 40; offset_rect.Height = offset_rect.Height + 40; //防止计算偏差后,有坐标溢出现象 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 GetBlackAndWhiteImage(DataTable particle,int picHeight, int picWidth, List segsData) { Bitmap dpImage=new Bitmap(picWidth, picHeight); using (Graphics graphics = Graphics.FromImage(dpImage)) { graphics.Clear(Color.White); // 清除位图背景为白色 } foreach (Segment seg in segsData) { int f_length = seg.Length; for (int m = 0; m < f_length; m++) { int lsjs_x = seg.Start + m; int lsjs_y = seg.Height; dpImage.SetPixel(lsjs_x, lsjs_y, Color.Black); } } DataRow row = particle.Rows[0]; Rectangle rectangle = new Rectangle() { X = Convert.ToInt32(row["RectLeft"]), Y = Convert.ToInt32(row["RectTop"]), Width = Convert.ToInt32(row["RectWidth"]), Height = Convert.ToInt32(row["RectHeight"]) }; Bitmap bmap = CapturePic(dpImage, rectangle); return bmap; } } }