C# CAD 2020 图纸 导出图片

 

 CAD

#region 照片提取
        public ImageFormat GetFormat(string filename)
        {
            // If all else fails, let's create a PNG
            // (might also choose to throw an exception)
            var imf = ImageFormat.Png;
            if (filename.Contains("."))
            {
                // Get the filename's extension (what follows the last ".")
                string ext = filename.Substring(filename.LastIndexOf(".") + 1);
                // Get the first three characters of the extension
                if (ext.Length > 3)
                    ext = ext.Substring(0, 3);
                // Choose the format based on the extension (in lowercase)
                switch (ext.ToLower())
                {
                    case "bmp":
                        imf = ImageFormat.Bmp;
                        break;

                    case "gif":
                        imf = ImageFormat.Gif;
                        break;

                    case "jpg":
                        imf = ImageFormat.Jpeg;
                        break;

                    case "tif":
                        imf = ImageFormat.Tiff;
                        break;

                    case "wmf":
                        imf = ImageFormat.Wmf;
                        break;

                    default:
                        imf = ImageFormat.Png;
                        break;
                }
            }
            return imf;
        }

        [CommandMethod("CPI")]
        public void CreatePreviewImage()
        {
            if (Document == null) return;
            var ed = Document.Editor;
            // Select the filename and type for our output image
            var pofo = new PromptSaveFileOptions("\nSelect image location");
            pofo.Filter =
              "Bitmap (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|" +
              "PNG (*.png)|*.png|TIFF (*.tif)|*.tif";
            // Set the default save location to be that of the current drawing
            string fn = Document.Database.Filename;
            if (fn.Contains("."))
            {
                int extIdx = fn.LastIndexOf(".");
                if (fn.Substring(extIdx + 1) != "dwt" && fn.Contains("\\"))
                {
                    pofo.InitialDirectory = Path.GetDirectoryName(Document.Database.Filename);
                }
            }

            var pfnr = ed.GetFileNameForSave(pofo);

            if (pfnr.Status != PromptStatus.OK) return;
            var outFile = pfnr.StringResult;
#if NET35

#else
            // Get the size of the document and capture the preview at that size
            var size = Document.Window.DeviceIndependentSize;
            using (var bmp = Document.CapturePreviewImage(Convert.ToUInt32(size.Width), Convert.ToUInt32(size.Height)))
            {
                // Save the file with the format derived from the filename
                bmp.Save(outFile, GetFormat(outFile));
            }
#endif
        }
        #endregion 照片提取

 

posted @ 2021-08-20 15:02  上帝视角  阅读(852)  评论(1编辑  收藏  举报