C#实现DWG图片预览

最近刚刚接触CAD二次开发,记录下开发的过程。查阅很多资料。

C#读取DWG文件

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("文件没有被找到");
            }
           
            FileStream DwgF=null;   //文件流
            int PosSentinel;   //文件描述块的位置
            BinaryReader br=null;   //读取二进制文件
            int TypePreview;   //缩略图格式
            int PosBMP;    //缩略图位置
            int LenBMP;    //缩略图大小
            short biBitCount; //缩略图比特深度
            BITMAPFILEHEADER biH; //BMP文件头,DWG文件中不包含位图文件头,要自行加上去
            byte[] BMPInfo;    //包含在DWG文件中的BMP文件体
            MemoryStream BMPF = new MemoryStream(); //保存位图的内存文件流
            BinaryWriter bmpr = new BinaryWriter(BMPF); //写二进制文件类
            System.Drawing.Image myImg = null;
            try
            {

                DwgF = new FileStream(FileName, FileMode.Open, FileAccess.Read); //文件流

                br = new BinaryReader(DwgF);
                DwgF.Seek(13, SeekOrigin.Begin); /

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要使用C#代码将图片导出为DWG格式,您需要使用CAD API,例如AutoCAD的API或者BricsCAD的API。下面是一个使用AutoCAD API的示例代码,它可以将指定的图片导出为DWG格式: ```csharp using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using System.IO; namespace ExportImageToDWG { public class Commands { [CommandMethod("ExportImageToDWG")] public static void ExportImageToDWG() { // Prompt the user to select the image file to be exported var promptResult = Application.DocumentManager.MdiActiveDocument.Editor.GetString("\nEnter the full path of the image file to be exported: "); if (promptResult.Status != PromptStatus.OK) return; var imagePath = promptResult.StringResult; // Prompt the user to select the DWG file to export to var fileDialog = new System.Windows.Forms.SaveFileDialog(); fileDialog.Filter = "DWG files (*.dwg)|*.dwg"; fileDialog.Title = "Export image to DWG"; var dialogResult = fileDialog.ShowDialog(); if (dialogResult != System.Windows.Forms.DialogResult.OK) return; var dwgPath = fileDialog.FileName; // Open the DWG file and create a new layout using (var db = new Database(false, true)) { db.ReadDwgFile(dwgPath, FileOpenMode.OpenForReadAndWriteNoShare, true, ""); using (var transaction = db.TransactionManager.StartTransaction()) { var layoutManager = LayoutManager.Current; var layout = new Layout(); layout.LayoutName = "Exported Image"; layoutManager.CreateLayout(layout.LayoutName); transaction.AddNewlyCreatedDBObject(layout, true); // Add the image to the layout var imageDef = new RasterImageDef(); imageDef.SourceFileName = imagePath; imageDef.Load(); var image = new RasterImage(); image.ImageDefId = imageDef.ObjectId; image.Rotation = 0; image.ScaleToFit = true; image.ShowImage = true; image.SetDatabaseDefaults(); layout.BlockTableRecordId.GetObject(OpenMode.ForWrite).AppendEntity(image); transaction.AddNewlyCreatedDBObject(image, true); // Commit the transaction and save the changes to the DWG file transaction.Commit(); db.SaveAs(dwgPath, DwgVersion.Current); } } // Inform the user that the export is complete Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nImage exported to " + dwgPath); } } } ``` 请注意,此示例代码仅供参考,您可能需要根据您的具体需求进行修改。另外,您需要将AutoCAD的API添加到您的项目中,以便使用AutoCAD的对象和方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值