BSEImgFileMgr.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //using CefSharp;
  2. //using Microsoft.Office.Interop.Word;
  3. using OTSCLRINTERFACE;
  4. using OTSDataType;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Forms;
  15. namespace OTSModelSharp
  16. {
  17. public class CBSEImageFileMgr
  18. {
  19. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  20. public struct BITMAPFILEHEADER
  21. {
  22. public UInt16 bfType;
  23. public UInt32 bfSize;
  24. public UInt16 bfReserved1;
  25. public UInt16 bfReserved2;
  26. public UInt32 bfOffBits;
  27. };
  28. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  29. public struct BITMAPINFOHEADER
  30. {
  31. public UInt32 biSize;
  32. public UInt32 biWidth;
  33. public UInt32 biHeight;
  34. public UInt16 biPlanes;
  35. public UInt16 biBitCount;
  36. public UInt16 biCompression;
  37. public UInt32 biSizeImage;
  38. public UInt32 biXPelsPerMeter;
  39. public UInt32 biYPelsPerMeter;
  40. public UInt32 biClrUsed;
  41. public UInt32 biClrImportant;
  42. };
  43. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  44. public struct RGBQUAD
  45. {
  46. public Byte rgbBlue;
  47. public Byte rgbGreen;
  48. public Byte rgbRed;
  49. public Byte rgbReserved;
  50. public Byte pColorTables;
  51. };
  52. // RGBQUAD[] pColorTable;
  53. // BSE image
  54. CBSEImgClr m_poBSE ;
  55. public String BMP_IMG_FILE_EXT = ".bmp";
  56. //System.Drawing.Size rect = new System.Drawing.Size();
  57. public String BSE_IMG_FILE_FILTER = "BSE image Files (*.img)|*.img||";
  58. // BSE image
  59. public String BSE_IMG_FILE_EXT = ".img";
  60. // file pathname
  61. String m_strPathName;
  62. public void CBSEImgFileMgr()
  63. {
  64. // initialization
  65. Init();
  66. }
  67. // initialization
  68. public void Init()
  69. {
  70. // initialization
  71. //m_poBSE = new CBSEImg();
  72. }
  73. // Load/Save
  74. public bool Load(String a_strPathName /*= _T("")*/, bool a_bClear /*= TRUE*/)
  75. {
  76. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  77. // clear all data if necessary
  78. if (a_bClear)
  79. {
  80. Init();
  81. }
  82. // check file pathname
  83. a_strPathName.Trim();
  84. if (a_strPathName == "")
  85. {
  86. // file open dialog
  87. OpenFileDialog openFileDialog = new OpenFileDialog();
  88. if (openFileDialog.ShowDialog() != DialogResult.OK)
  89. {
  90. return false;
  91. }
  92. // get file pathname
  93. openFileDialog.Filter = BSE_IMG_FILE_FILTER;
  94. a_strPathName = openFileDialog.FileName;
  95. }
  96. // open the particle analysis standard file
  97. if (!File.Exists(a_strPathName))
  98. {
  99. // failed to open the file
  100. return false;
  101. }
  102. FileStream NewText = File.Create(a_strPathName);
  103. NewText.Close();
  104. // file pathname
  105. m_strPathName = a_strPathName;
  106. // ok, return TRUE
  107. return true;
  108. }
  109. public bool LoadFromBitmap(String a_strPathName /*= _T("")*/, bool a_bClear/* = TRUE*/)
  110. {
  111. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  112. // prepare file
  113. if (a_bClear)
  114. {
  115. Init();
  116. }
  117. // check file pathname
  118. a_strPathName.Trim();
  119. if (a_strPathName == "")
  120. {
  121. // file open dialog
  122. OpenFileDialog openFileDialog = new OpenFileDialog();
  123. if (openFileDialog.ShowDialog() != DialogResult.OK)
  124. {
  125. return false;
  126. }
  127. // get file pathname
  128. openFileDialog.Filter = BSE_IMG_FILE_FILTER;
  129. a_strPathName = openFileDialog.FileName;
  130. }
  131. else
  132. {
  133. m_strPathName = a_strPathName;
  134. }
  135. Byte[] pPixel ;
  136. Byte[] pBmInfo;
  137. BITMAPFILEHEADER pBmfh;
  138. pBmfh = new BITMAPFILEHEADER();
  139. OpenFileDialog openFileDialogs = new OpenFileDialog();
  140. if (openFileDialogs.ShowDialog() != DialogResult.OK)
  141. {
  142. return false;
  143. }
  144. // read file header
  145. pBmInfo = new Byte[pBmfh.bfOffBits - 14];
  146. FileStream aFile = new FileStream(pBmfh.ToString(), FileMode.Open);
  147. aFile.Read(pBmInfo, 0, 200);
  148. BITMAPINFOHEADER bmih = new BITMAPINFOHEADER();
  149. File.Copy(bmih.ToString(), pBmInfo.ToString());
  150. long width = bmih.biWidth;//获取宽度
  151. int bitCount = bmih.biBitCount;//获取位数
  152. long height = bmih.biHeight;//获取高度
  153. long LineBytes = (width * bitCount + 31) / 32 * 4;//计算每行像素所占的字节数
  154. pPixel = new Byte[height * LineBytes];
  155. aFile.Read(pPixel, 0, (int)height * (int)LineBytes);
  156. aFile.Close();
  157. System.Drawing.Rectangle images = new System.Drawing.Rectangle(0, 0, (int)width - 0, 0 - (int)height);
  158. m_poBSE.SetImageRect(images);
  159. m_poBSE.InitImageData(images.Width, images.Height);
  160. Byte[] pImageData = m_poBSE.GetImageDataPtr();
  161. long nActImageSize = width * height;
  162. for (int i = 0; i < height; i++)
  163. {
  164. File.Copy(pImageData.ToString() + i * width, pPixel.ToString() + (height - 1 - i) * width);
  165. }
  166. File.Delete(pPixel.ToString());
  167. File.Delete(pBmInfo.ToString());
  168. File.Delete(pBmfh.ToString());
  169. return true;
  170. }
  171. public bool SaveIntoBitmap(String a_strPathName)
  172. {
  173. // check file pathname
  174. a_strPathName.Trim();
  175. if (a_strPathName == "")
  176. {
  177. // file open dialog
  178. OpenFileDialog openFileDialog = new OpenFileDialog();
  179. if (openFileDialog.ShowDialog() != DialogResult.OK)
  180. {
  181. return false;
  182. }
  183. // get file pathname
  184. openFileDialog.Filter = BSE_IMG_FILE_FILTER;
  185. a_strPathName = openFileDialog.FileName;
  186. }
  187. Bitmap rstBm = CreateBitmap(m_poBSE.GetImageDataPtr(), m_poBSE.GetWidth(), m_poBSE.GetHeight());
  188. if (rstBm == null)
  189. {
  190. return false;
  191. }
  192. rstBm.Save(a_strPathName);
  193. return true;
  194. }
  195. byte[] StructureToByteArray(object obj)
  196. {
  197. int len = Marshal.SizeOf(obj);
  198. byte[] arr = new byte[len];
  199. IntPtr ptr = Marshal.AllocHGlobal(len);
  200. Marshal.StructureToPtr(obj, ptr, true);
  201. Marshal.Copy(ptr, arr, 0, len);
  202. Marshal.FreeHGlobal(ptr);
  203. return arr;
  204. }
  205. /// <summary>
  206. /// 使用byte[]数据,生成256色灰度 BMP 位图
  207. /// </summary>
  208. /// <param name="originalImageData"></param>
  209. /// <param name="originalWidth"></param>
  210. /// <param name="originalHeight"></param>
  211. /// <returns></returns>
  212. public static Bitmap CreateBitmap(byte[] originalImageData, int originalWidth, int originalHeight)
  213. {
  214. if (originalImageData == null || originalWidth == 0 || originalHeight == 0)
  215. {
  216. return null;
  217. }
  218. //指定8位格式,即256色
  219. Bitmap resultBitmap = new Bitmap(originalWidth, originalHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
  220. //将该位图存入内存中
  221. MemoryStream curImageStream = new MemoryStream();
  222. resultBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Bmp);
  223. curImageStream.Flush();
  224. //由于位图数据需要DWORD对齐(4byte倍数),计算需要补位的个数
  225. int curPadNum = ((originalWidth * 8 + 31) / 32 * 4) - originalWidth;
  226. //最终生成的位图数据大小
  227. int bitmapDataSize = ((originalWidth * 8 + 31) / 32 * 4) * originalHeight;
  228. //数据部分相对文件开始偏移,具体可以参考位图文件格式
  229. int dataOffset = ReadData(curImageStream, 10, 4);
  230. //改变调色板,因为默认的调色板是32位彩色的,需要修改为256色的调色板
  231. int paletteStart = 54;
  232. int paletteEnd = dataOffset;
  233. int color = 0;
  234. for (int i = paletteStart; i < paletteEnd; i += 4)
  235. {
  236. byte[] tempColor = new byte[4];
  237. tempColor[0] = (byte)color;
  238. tempColor[1] = (byte)color;
  239. tempColor[2] = (byte)color;
  240. tempColor[3] = (byte)0;
  241. color++;
  242. curImageStream.Position = i;
  243. curImageStream.Write(tempColor, 0, 4);
  244. }
  245. //最终生成的位图数据,以及大小,高度没有变,宽度需要调整
  246. byte[] destImageData = new byte[bitmapDataSize];
  247. int destWidth = originalWidth + curPadNum;
  248. //生成最终的位图数据,注意的是,位图数据 从左到右,从下到上,所以需要颠倒
  249. for (int originalRowIndex = originalHeight - 1; originalRowIndex >= 0; originalRowIndex--)
  250. {
  251. int destRowIndex = originalHeight - originalRowIndex - 1;
  252. for (int dataIndex = 0; dataIndex < originalWidth; dataIndex++)
  253. {
  254. //同时还要注意,新的位图数据的宽度已经变化destWidth,否则会产生错位
  255. destImageData[destRowIndex * destWidth + dataIndex] = originalImageData[originalRowIndex * originalWidth + dataIndex];
  256. }
  257. }
  258. //将流的Position移到数据段
  259. curImageStream.Position = dataOffset;
  260. //将新位图数据写入内存中
  261. curImageStream.Write(destImageData, 0, bitmapDataSize);
  262. curImageStream.Flush();
  263. //将内存中的位图写入Bitmap对象
  264. resultBitmap = new Bitmap(curImageStream);
  265. return resultBitmap;
  266. }
  267. /// <summary>
  268. /// 从内存流中指定位置,读取数据
  269. /// </summary>
  270. /// <param name="curStream"></param>
  271. /// <param name="startPosition"></param>
  272. /// <param name="length"></param>
  273. /// <returns></returns>
  274. public static int ReadData(MemoryStream curStream, int startPosition, int length)
  275. {
  276. int result = -1;
  277. byte[] tempData = new byte[length];
  278. curStream.Position = startPosition;
  279. curStream.Read(tempData, 0, length);
  280. result = BitConverter.ToInt32(tempData, 0);
  281. return result;
  282. }
  283. /// <summary>
  284. /// 向内存流中指定位置,写入数据
  285. /// </summary>
  286. /// <param name="curStream"></param>
  287. /// <param name="startPosition"></param>
  288. /// <param name="length"></param>
  289. /// <param name="value"></param>
  290. public static void WriteData(MemoryStream curStream, int startPosition, int length, int value)
  291. {
  292. curStream.Position = startPosition;
  293. curStream.Write(BitConverter.GetBytes(value), 0, length);
  294. }
  295. // file pathname
  296. public String GetPathName()
  297. {
  298. return m_strPathName;
  299. }
  300. public void SetPathName(String a_strPathName)
  301. {
  302. m_strPathName = a_strPathName;
  303. }
  304. //image
  305. public CBSEImgClr GetBSEImg()
  306. {
  307. return m_poBSE;
  308. }
  309. public void SetBSEImg(CBSEImgClr a_poBSE)
  310. {
  311. m_poBSE = a_poBSE;
  312. }
  313. }
  314. }