123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace OTSCommon
- {
- public class ParticleData
- {
- private SqLiteHelper dbHelper = null;
- public ParticleData(string path)
- {
- dbHelper = new SqLiteHelper("data source='" + path + "\\FIELD_FILES\\Inclution.db'");
- }
- /// <summary>
- /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
- /// </summary>
- /// <param name="in_cotsparticleclr"></param>
- /// <returns></returns>
- public Bitmap GetBitmapByParticle(string fieldImage, Rectangle offset_rect)
- {
- //然后将取出的数据,转换成Bitmap对象
- Bitmap ls_bt = ReadImageFile(fieldImage);
- //为了能把整个颗粒显示完整
- 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, PixelFormat.Format8bppIndexed);
- }
- else
- {
- new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
- }
- //in_cotsparticleclr.Dispose();
- return new_ret_bp;
- }
- /// <summary>
- /// 获取颗粒全部所需信息
- /// </summary>
- /// <param name=""></param>
- /// <param name=""></param>
- /// <returns></returns>
- public DataTable GetAllInfo(string condition)
- {
- //string sqliteString = "select fieldid, particleid, AveGray, RectLeft, RectTop, RectWidth, RectHeight, Area, PosX, PosY, TypeId, ElementNum, SegmentNum, FieldPosX, FieldPosY, ParticleId, DMAX, DMIN, DPERP, PERIMETER, ORIENTATION, DINSCR, DMEAN, DELONG, DFERET, TypeName, TypeColor, SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticles.XRayId and fieldid = MergedParticles.fieldid) as Element from MergedParticles union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = INcAData.XRayId and fieldid = INcAData.fieldid ) as Element from INcAData where xrayid > -1 and instr(',' || (select ifnull(group_concat(SubParticles, '&'), '') from MergedParticles)|| ',',',' || fieldid || ':' || particleid || ',')= 0 order by fieldid,particleid";
- string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticles.XRayId and fieldid = MergedParticles.fieldid) as Element from MergedParticles where 1=1 " + condition + " union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = INcAData.XRayId and fieldid = INcAData.fieldid ) as Element from INcAData where xrayid > -1 and instr(','||(select ifnull(group_concat(SubParticles, '&'),'') from MergedParticles)|| ',',',' || fieldid || ':' || particleid || ',')= 0 " + condition;
- DataTable DT = dbHelper.ExecuteQuery(sqliteString);
- return DT;
- }
- /// <summary>
- /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public Bitmap ReadImageFile(string path)
- {
- if (!File.Exists(path))
- {
- return null;//文件不存在
- }
- FileStream fs = File.OpenRead(path); //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=""></param>
- /// <param name=""></param>
- /// <returns></returns>
- public DataTable GetInfoForPartucleDevidePage(int currentPage, int pagesize, string OrderFunction)
- {
- int p = (currentPage - 1) * pagesize;
- string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticles.XRayId and fieldid = MergedParticles.fieldid) as Element from MergedParticles union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = INcAData.XRayId and fieldid = INcAData.fieldid ) as Element from INcAData where xrayid > -1 and instr(','||(select ifnull(group_concat(SubParticles, '&'),'') from MergedParticles)|| ',',',' || fieldid || ':' || particleid || ',')= 0 order by "+ OrderFunction + " limit " + pagesize.ToString() + " offset " + p.ToString();
- DataTable DT = dbHelper.ExecuteQuery(sqliteString);
- return DT;
- }
- /// <summary>
- /// 获取分页查询所需信息
- /// </summary>
- /// <param name=""></param>
- /// <param name=""></param>
- /// <returns></returns>
- public DataTable GetInfoForPartucleDevidePage(int currentPage, int pagesize, string OrderFunction,string condition)
- {
- int p = (currentPage - 1) * pagesize;
- string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticles.XRayId and fieldid = MergedParticles.fieldid) as Element from MergedParticles where 1=1 "+condition+" union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = INcAData.XRayId and fieldid = INcAData.fieldid ) as Element from INcAData where xrayid > -1 and instr(','||(select ifnull(group_concat(SubParticles, '&'),'') from MergedParticles)|| ',',',' || fieldid || ':' || particleid || ',')= 0 "+ condition +" order by " + OrderFunction + " limit " + pagesize.ToString() + " offset " + p.ToString();
- DataTable DT = dbHelper.ExecuteQuery(sqliteString);
- return DT;
- }
- //public Bitmap GetBitmapForBig(string sub, double xs, string path,int currentPage, int pagesize, string OrderFunction, string condition)
- //{
- // string vs = "," + sub.Replace(':', '-') + ",";
- // DataTable dataTable = GetInfoForPartucleDevidePage(currentPage, pagesize, OrderFunction, condition);
- // if (dataTable.Rows.Count == 0)
- // {
- // return null;
- // }
- // //内接矩形
- // double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
- // double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
- // double min_Y = max_Y;
- // double min_X = max_X;
- // //拼接field矩形
- // double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
- // double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
- // double MIN_Y = MAX_Y;
- // double MIN_X = MAX_X;
- // foreach (DataRow item in dataTable.Rows)
- // {
- // //颗粒外接矩形
- // double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + 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 = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
- // if (max_Y < lrfttopY)
- // {
- // max_Y = lrfttopY;
- // }
- // if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
- // {
- // min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
- // }
- // //画布
- // double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
- // if (lefttopXH > MAX_X)
- // {
- // MAX_X = lefttopXH;
- // }
- // if (lefttopXH < MIN_X)
- // {
- // MIN_X = lefttopXH;
- // }
- // double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
- // if (MAX_Y < lrfttopYH)
- // {
- // MAX_Y = lrfttopYH;
- // }
- // if (MIN_Y > lrfttopYH)
- // {
- // MIN_Y = lrfttopYH;
- // }
- // }
- // int WIDTH = Convert.ToInt32(MAX_X - MIN_X) + 1024;
- // int HEIGHT = Convert.ToInt32(MAX_Y - MIN_Y) + 768;
- // //构造最终的图片白板
- // Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
- // Graphics graph = Graphics.FromImage(tableChartImage);
- // //初始化这个大图
- // graph.DrawImage(tableChartImage, 0, 0);
- // int width = Convert.ToInt32(max_X - min_X);
- // int height = Convert.ToInt32(max_Y - min_Y);
- // int X = Convert.ToInt32(min_X - MIN_X);
- // int Y = Convert.ToInt32(MAX_Y - max_Y);
- // Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
- // foreach (DataRow item in dataTable.Rows)
- // {
- // string filePath = path + "\\FIELD_FILES\\";
- // string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
- // //然后将取出的数据,转换成Bitmap对象
- // Bitmap ls_bt = ReadImageFile(imagePath);
- // int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
- // int y = Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MIN_Y);
- // try
- // {
- // graph.DrawImage(ls_bt, x, y);
- // }
- // catch (Exception e)
- // {
- // throw;
- // }
- // }
- // Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
- // // bmap.Save(path + "asd.bmp");
- // return bmap;
- //}
- /// <summary>
- /// 获取所有Particle
- /// </summary>
- /// <returns></returns>
- public DataTable GetParticleAll(string fieldAndPartic)
- {
- string sqlp = @"select * from INcAData";
- if (fieldAndPartic != "")
- {
- sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
- }
- DataTable DT = dbHelper.ExecuteQuery(sqlp);
- return DT;
- }
- public Bitmap GetBitmapForBig(string sub, double xs, string path)
- {
- string vs = "," + sub.Replace(':', '-') + ",";
- DataTable dataTable = GetParticleAll(vs);
- if (dataTable.Rows.Count == 0)
- {
- return null;
- }
- //内接矩形
- double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
- double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
- double min_Y = max_Y;
- double min_X = max_X;
- //拼接field矩形
- double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
- double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
- double MIN_Y = MAX_Y;
- double MIN_X = MAX_X;
- foreach (DataRow item in dataTable.Rows)
- {
- //颗粒外接矩形
- double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + 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 = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
- if (max_Y < lrfttopY)
- {
- max_Y = lrfttopY;
- }
- if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
- {
- min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
- }
- //画布
- double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
- if (lefttopXH > MAX_X)
- {
- MAX_X = lefttopXH;
- }
- if (lefttopXH < MIN_X)
- {
- MIN_X = lefttopXH;
- }
- double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
- if (MAX_Y < lrfttopYH)
- {
- MAX_Y = lrfttopYH;
- }
- if (MIN_Y > lrfttopYH)
- {
- MIN_Y = lrfttopYH;
- }
- }
- int WIDTH = System.Math.Abs(Convert.ToInt32(MAX_X - MIN_X)) + 1024;
- int HEIGHT = System.Math.Abs(Convert.ToInt32(MAX_Y - MIN_Y)) + 768;
- //构造最终的图片白板
- Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
- Graphics graph = Graphics.FromImage(tableChartImage);
- //初始化这个大图
- graph.DrawImage(tableChartImage, 0, 0);
- int width = Convert.ToInt32(max_X - min_X);
- int height = Convert.ToInt32(max_Y - min_Y);
- int X = Convert.ToInt32(min_X - MIN_X);
- int Y = Convert.ToInt32(MAX_Y - max_Y);
- Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
- foreach (DataRow item in dataTable.Rows)
- {
- string filePath = path + "\\FIELD_FILES\\";
- string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
- //然后将取出的数据,转换成Bitmap对象
- Bitmap ls_bt = ReadImageFile(imagePath);
- int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
- int y = System.Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MAX_Y));
- try
- {
- graph.DrawImage(ls_bt, x, y);
- }
- catch /*(Exception e)*/
- {
- throw;
- }
- }
- Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
- //bmap.Save(path + "asd.bmp");
- return bmap;
- }
- /// <summary>
- /// /// tbale转list
- /// /// </summary>
- /// /// <typeparam name="T"></typeparam>
- /// /// <returns></returns>
- public List<T> TableToList<T>(DataTable table) where T : class, new()
- {
- var result = new List<T>();
- var propertys = typeof(T).GetProperties();
- foreach (DataRow dr in table.Rows)
- {
- var item = new T();
- result.Add(item);
- foreach (var pi in propertys)
- {
- if (!table.Columns.Contains(pi.Name))
- continue;
- var value = dr[pi.Name];
- if (value is DBNull || value == null)
- continue;
- if (value.GetType().ToString() == "System.Int64")
- {
- pi.SetValue(item, Convert.ToInt32(value));
- }
- else
- {
- pi.SetValue(item, value);
- }
- }
- }
- return result;
- }
- }
- }
|