//using CefSharp; //using Microsoft.Office.Interop.Word; using OTSCLRINTERFACE; using OTSDataType; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; namespace OTSModelSharp { public class CBSEImageFileMgr { [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BITMAPFILEHEADER { public UInt16 bfType; public UInt32 bfSize; public UInt16 bfReserved1; public UInt16 bfReserved2; public UInt32 bfOffBits; }; [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BITMAPINFOHEADER { public UInt32 biSize; public UInt32 biWidth; public UInt32 biHeight; public UInt16 biPlanes; public UInt16 biBitCount; public UInt16 biCompression; public UInt32 biSizeImage; public UInt32 biXPelsPerMeter; public UInt32 biYPelsPerMeter; public UInt32 biClrUsed; public UInt32 biClrImportant; }; [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct RGBQUAD { public Byte rgbBlue; public Byte rgbGreen; public Byte rgbRed; public Byte rgbReserved; public Byte pColorTables; }; // RGBQUAD[] pColorTable; // BSE image CBSEImgClr m_poBSE ; public String BMP_IMG_FILE_EXT = ".bmp"; //System.Drawing.Size rect = new System.Drawing.Size(); public String BSE_IMG_FILE_FILTER = "BSE image Files (*.img)|*.img||"; // BSE image public String BSE_IMG_FILE_EXT = ".img"; // file pathname String m_strPathName; public void CBSEImgFileMgr() { // initialization Init(); } // initialization public void Init() { // initialization //m_poBSE = new CBSEImg(); } // Load/Save public bool Load(String a_strPathName /*= _T("")*/, bool a_bClear /*= TRUE*/) { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // clear all data if necessary if (a_bClear) { Init(); } // check file pathname a_strPathName.Trim(); if (a_strPathName == "") { // file open dialog OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() != DialogResult.OK) { return false; } // get file pathname openFileDialog.Filter = BSE_IMG_FILE_FILTER; a_strPathName = openFileDialog.FileName; } // open the particle analysis standard file if (!File.Exists(a_strPathName)) { // failed to open the file return false; } FileStream NewText = File.Create(a_strPathName); NewText.Close(); // file pathname m_strPathName = a_strPathName; // ok, return TRUE return true; } public bool LoadFromBitmap(String a_strPathName /*= _T("")*/, bool a_bClear/* = TRUE*/) { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // prepare file if (a_bClear) { Init(); } // check file pathname a_strPathName.Trim(); if (a_strPathName == "") { // file open dialog OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() != DialogResult.OK) { return false; } // get file pathname openFileDialog.Filter = BSE_IMG_FILE_FILTER; a_strPathName = openFileDialog.FileName; } else { m_strPathName = a_strPathName; } Byte[] pPixel ; Byte[] pBmInfo; BITMAPFILEHEADER pBmfh; pBmfh = new BITMAPFILEHEADER(); OpenFileDialog openFileDialogs = new OpenFileDialog(); if (openFileDialogs.ShowDialog() != DialogResult.OK) { return false; } // read file header pBmInfo = new Byte[pBmfh.bfOffBits - 14]; FileStream aFile = new FileStream(pBmfh.ToString(), FileMode.Open); aFile.Read(pBmInfo, 0, 200); BITMAPINFOHEADER bmih = new BITMAPINFOHEADER(); File.Copy(bmih.ToString(), pBmInfo.ToString()); long width = bmih.biWidth;//获取宽度 int bitCount = bmih.biBitCount;//获取位数 long height = bmih.biHeight;//获取高度 long LineBytes = (width * bitCount + 31) / 32 * 4;//计算每行像素所占的字节数 pPixel = new Byte[height * LineBytes]; aFile.Read(pPixel, 0, (int)height * (int)LineBytes); aFile.Close(); System.Drawing.Rectangle images = new System.Drawing.Rectangle(0, 0, (int)width - 0, 0 - (int)height); m_poBSE.SetImageRect(images); m_poBSE.InitImageData(images.Width, images.Height); Byte[] pImageData = m_poBSE.GetImageDataPtr(); long nActImageSize = width * height; for (int i = 0; i < height; i++) { File.Copy(pImageData.ToString() + i * width, pPixel.ToString() + (height - 1 - i) * width); } File.Delete(pPixel.ToString()); File.Delete(pBmInfo.ToString()); File.Delete(pBmfh.ToString()); return true; } public bool SaveIntoBitmap(String a_strPathName) { // check file pathname a_strPathName.Trim(); if (a_strPathName == "") { // file open dialog OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() != DialogResult.OK) { return false; } // get file pathname openFileDialog.Filter = BSE_IMG_FILE_FILTER; a_strPathName = openFileDialog.FileName; } Bitmap rstBm = CreateBitmap(m_poBSE.GetImageDataPtr(), m_poBSE.GetWidth(), m_poBSE.GetHeight()); if (rstBm == null) { return false; } rstBm.Save(a_strPathName); return true; } byte[] StructureToByteArray(object obj) { int len = Marshal.SizeOf(obj); byte[] arr = new byte[len]; IntPtr ptr = Marshal.AllocHGlobal(len); Marshal.StructureToPtr(obj, ptr, true); Marshal.Copy(ptr, arr, 0, len); Marshal.FreeHGlobal(ptr); return arr; } /// /// 使用byte[]数据,生成256色灰度 BMP 位图 /// /// /// /// /// public static Bitmap CreateBitmap(byte[] originalImageData, int originalWidth, int originalHeight) { if (originalImageData == null || originalWidth == 0 || originalHeight == 0) { return null; } //指定8位格式,即256色 Bitmap resultBitmap = new Bitmap(originalWidth, originalHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); //将该位图存入内存中 MemoryStream curImageStream = new MemoryStream(); resultBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Bmp); curImageStream.Flush(); //由于位图数据需要DWORD对齐(4byte倍数),计算需要补位的个数 int curPadNum = ((originalWidth * 8 + 31) / 32 * 4) - originalWidth; //最终生成的位图数据大小 int bitmapDataSize = ((originalWidth * 8 + 31) / 32 * 4) * originalHeight; //数据部分相对文件开始偏移,具体可以参考位图文件格式 int dataOffset = ReadData(curImageStream, 10, 4); //改变调色板,因为默认的调色板是32位彩色的,需要修改为256色的调色板 int paletteStart = 54; int paletteEnd = dataOffset; int color = 0; for (int i = paletteStart; i < paletteEnd; i += 4) { byte[] tempColor = new byte[4]; tempColor[0] = (byte)color; tempColor[1] = (byte)color; tempColor[2] = (byte)color; tempColor[3] = (byte)0; color++; curImageStream.Position = i; curImageStream.Write(tempColor, 0, 4); } //最终生成的位图数据,以及大小,高度没有变,宽度需要调整 byte[] destImageData = new byte[bitmapDataSize]; int destWidth = originalWidth + curPadNum; //生成最终的位图数据,注意的是,位图数据 从左到右,从下到上,所以需要颠倒 for (int originalRowIndex = originalHeight - 1; originalRowIndex >= 0; originalRowIndex--) { int destRowIndex = originalHeight - originalRowIndex - 1; for (int dataIndex = 0; dataIndex < originalWidth; dataIndex++) { //同时还要注意,新的位图数据的宽度已经变化destWidth,否则会产生错位 destImageData[destRowIndex * destWidth + dataIndex] = originalImageData[originalRowIndex * originalWidth + dataIndex]; } } //将流的Position移到数据段 curImageStream.Position = dataOffset; //将新位图数据写入内存中 curImageStream.Write(destImageData, 0, bitmapDataSize); curImageStream.Flush(); //将内存中的位图写入Bitmap对象 resultBitmap = new Bitmap(curImageStream); return resultBitmap; } /// /// 从内存流中指定位置,读取数据 /// /// /// /// /// public static int ReadData(MemoryStream curStream, int startPosition, int length) { int result = -1; byte[] tempData = new byte[length]; curStream.Position = startPosition; curStream.Read(tempData, 0, length); result = BitConverter.ToInt32(tempData, 0); return result; } /// /// 向内存流中指定位置,写入数据 /// /// /// /// /// public static void WriteData(MemoryStream curStream, int startPosition, int length, int value) { curStream.Position = startPosition; curStream.Write(BitConverter.GetBytes(value), 0, length); } // file pathname public String GetPathName() { return m_strPathName; } public void SetPathName(String a_strPathName) { m_strPathName = a_strPathName; } //image public CBSEImgClr GetBSEImg() { return m_poBSE; } public void SetBSEImg(CBSEImgClr a_poBSE) { m_poBSE = a_poBSE; } } }