| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- 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\";
- }
- /// <summary>
- /// 拼接颗粒黑白图
- /// </summary>
- /// <param name="sub">拼接字符串</param>
- /// <param name="xs">xs = pixw / ScanFieldSize</param>
- /// <param name="path">报告文件路径</param>
- /// <param name="picHeight">图像高</param>
- /// <param name="picWidth">帧图宽</param>
- /// <param name="segsData">KEY为FidANDpID</param>
- /// <returns></returns>
- public Bitmap GetBlackAndWhiteBitmapForMergedParticle(string sub, double xs, int picHeight, int picWidth,DataTable PartdataTable,Dictionary<string,List<Segment>> 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;
- }
- /// <summary>
- /// 拼接颗粒图
- /// </summary>
- /// <param name="sub">拼接字符串</param>
- /// <param name="xs">xs = pixw / ScanFieldSize</param>
- /// <param name="path">报告文件路径</param>
- /// <param name="picHeight">图像高</param>
- /// <param name="picWidth">帧图宽</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
- /// </summary>
- /// <param name="in_cotsparticleclr"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取全部颗粒图
- /// </summary>
- /// <param name="sub">拼接颗粒连接字符串</param>
- /// <param name="dataTable">颗粒表</param>
- /// <param name="xs">pixel</param>
- /// <param name="picHeight">帧图高</param>
- /// <param name="picWidth">帧图宽</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取全部颗粒图
- /// </summary>
- /// <param name="sub">拼接颗粒连接字符串</param>
- /// <param name="dataTable">颗粒表</param>
- /// <param name="xs">pixel</param>
- /// <param name="picHeight">帧图高</param>
- /// <param name="picWidth">帧图宽</param>
- /// <returns></returns>
- public Bitmap GetBitmapForParticleAllWithBlackAndWhite(string sub, DataTable dataTable, Dictionary<string, List<Segment>> segsData, double xs = 0, int picHeight = 0, int picWidth = 0)
- {
- Bitmap bmap;
- if (sub != "")
- {
- bmap = GetBlackAndWhiteBitmapForMergedParticle(sub, xs, picHeight, picWidth, dataTable, segsData);
- }
- else
- {
- List<Segment> 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<Segment> 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;
- }
- }
- }
|