狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#預(yù)覽DWG文件方法:直接讀取文件結(jié)構(gòu)

admin
2021年12月14日 10:28 本文熱度 2335
struct BITMAPFILEHEADER
        {
            public short bfType;
            public int bfSize;
            public short bfReserved1;
            public short bfReserved2;
            public int bfOffBits;
        }



        public static System.Drawing.Image GetDwgImage(string FileName)
        {
            if (!(File.Exists(FileName)))
            {
                throw new FileNotFoundException("文件沒(méi)有被找到");
            }
            FileStream DwgF=null;   //文件流
            int PosSentinel;   //文件描述塊的位置
            BinaryReader br=null;   //讀取二進(jìn)制文件
            int TypePreview;   //縮略圖格式
            int PosBMP;    //縮略圖位置 
            int LenBMP;    //縮略圖大小
            short biBitCount; //縮略圖比特深度 
            BITMAPFILEHEADER biH; //BMP文件頭,DWG文件中不包含位圖文件頭,要自行加上去
            byte[] BMPInfo;    //包含在DWG文件中的BMP文件體
            MemoryStream BMPF = new MemoryStream(); //保存位圖的內(nèi)存文件流
            BinaryWriter bmpr = new BinaryWriter(BMPF); //寫(xiě)二進(jìn)制文件類
            System.Drawing.Image myImg = null;
            try
            {
                DwgF = new FileStream(FileName, FileMode.Open, FileAccess.Read); //文件流
                br = new BinaryReader(DwgF);
                DwgF.Seek(13, SeekOrigin.Begin); //從第十三字節(jié)開(kāi)始讀取
                PosSentinel = br.ReadInt32();   //1317字節(jié)指示縮略圖描述塊的位置
                DwgF.Seek(PosSentinel + 30, SeekOrigin.Begin);   //將指針移到縮略圖描述塊的第31字節(jié)
                TypePreview = br.ReadByte();   //31字節(jié)為縮略圖格式信息,2 BMP格式,3WMF格式
                if (TypePreview == 1)
                {
                }
                else if (TypePreview == 2 || TypePreview == 3)
                {
                    PosBMP = br.ReadInt32(); //DWG文件保存的位圖所在位置
                    LenBMP = br.ReadInt32(); //位圖的大小
                    DwgF.Seek(PosBMP + 14, SeekOrigin.Begin); //移動(dòng)指針到位圖塊
                    biBitCount = br.ReadInt16(); //讀取比特深度
                    DwgF.Seek(PosBMP, SeekOrigin.Begin); //從位圖塊開(kāi)始處讀取全部位圖內(nèi)容備用
                    BMPInfo = br.ReadBytes(LenBMP); //不包含文件頭的位圖信息
                    br.Close();
                    DwgF.Close();
                    biH.bfType = 19778; //建立位圖文件頭
                    if (biBitCount < 9)
                    {
                        biH.bfSize = 54 + 4 * (int)(Math.Pow(2, biBitCount)) + LenBMP;
                    }
                    else
                    {
                        biH.bfSize = 54 + LenBMP;
                    }
                    biH.bfReserved1 = 0; //保留字節(jié)
                    biH.bfReserved2 = 0; //保留字節(jié)
                    biH.bfOffBits = 14 + 40 + 1024; //圖像數(shù)據(jù)偏移
                    //以下開(kāi)始寫(xiě)入位圖文件頭
                    bmpr.Write(biH.bfType); //文件類型
                    bmpr.Write(biH.bfSize);   //文件大小
                    bmpr.Write(biH.bfReserved1); //0
                    bmpr.Write(biH.bfReserved2); //0
                    bmpr.Write(biH.bfOffBits); //圖像數(shù)據(jù)偏移
                    bmpr.Write(BMPInfo); //寫(xiě)入位圖
                    BMPF.Seek(0, SeekOrigin.Begin); //指針移到文件開(kāi)始處 
                    myImg = System.Drawing.Image.FromStream(BMPF); //創(chuàng)建位圖文件對(duì)象
                    bmpr.Close();
                    BMPF.Close();
                }
                return myImg;
            }
            catch (EndOfStreamException)
            {
                throw new EndOfStreamException("文件不是標(biāo)準(zhǔn)的DWG格式文件,無(wú)法預(yù)覽!");
            }
            catch (IOException ex)
            {
                if (ex.Message == "試圖將文件指針移到文件開(kāi)頭之前。/r/n")
                {
                    throw new IOException("文件不是標(biāo)準(zhǔn)的DWG格式文件,無(wú)法預(yù)覽!");
                }
                else if (ex.Message == "文件“" + FileName + "”正由另一進(jìn)程使用,因此該進(jìn)程無(wú)法訪問(wèn)該文件。")
                {
                    //復(fù)制文件,繼續(xù)預(yù)覽
                    File.Copy(FileName, Application.StartupPath + @"/linshi.dwg", true);
                    Image image = GetDwgImage(Application.StartupPath + @"/linshi.dwg");
                    File.Delete(Application.StartupPath + @"/linshi.dwg");
                    return image;
                }
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (DwgF != null)
                {
                    DwgF.Close();
                }
                if (br != null)
                {
                    br.Close();
                }
                bmpr.Close();
                BMPF.Close();
            }
        }



讀取出來(lái)的背景色為白色,效果比較差,很多顏色顯示不出來(lái),當(dāng)時(shí)認(rèn)為顯示DWG文件出錯(cuò)誤了,問(wèn)了些高手,(呵呵,別人告訴自己本身取出的就是白色背景,需要自己改變背景色,在此鄙視一下自己)所以繼續(xù)用C#操作返回的IMAGE對(duì)象,改變背景色
///<summary>
        ///顯示DWG文件
        ///</summary>
        ///<param name="Pwidth">要顯示的寬度</param>
        ///<param name="PHeight">要顯示的高度</param>
        ///<returns></returns>
        public static System.Drawing.Image ShowDWG(int Pwidth,int PHeight,string FilePath)
        {
            System.Drawing.Image image = GetDwgImage(FilePath);
            Bitmap bitmap = new Bitmap(image);
            int Height = bitmap.Height;
            int Width = bitmap.Width;
            Bitmap newbitmap = new Bitmap(Width, Height);
            Bitmap oldbitmap = (Bitmap)bitmap;
            Color pixel;
            for (int x = 1; x < Width; x++)
            {
                for (int y = 1; y < Height; y++)
                {
                    pixel = oldbitmap.GetPixel(x, y);
                    int r = pixel.R, g = pixel.G, b = pixel.B;
                    if (pixel.Name == "ffffffff" || pixel.Name == "ff000000")
                    {
                        r = 255 - pixel.R;
                        g = 255 - pixel.G;
                        b = 255 - pixel.B;
                    }
                    newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            Bitmap bt = new Bitmap(newbitmap, Pwidth, PHeight);
            return newbitmap;
        }

該文章在 2021/12/14 10:31:33 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved