乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 96|回复: 5

KEAN 的c#截图程序,哪位用c#帮忙编译一下

[复制链接]

31

主题

83

帖子

6

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
207
发表于 2013-6-13 08:50:00 | 显示全部楼层 |阅读模式
KEAN 的c#截图程序,哪位用c#帮忙编译一下
这个功能如果autodesk公司做的话应该是小菜一碟。me.support@autodesk.com  期待...
1 using Autodesk..ApplicationServices;
    2 using Autodesk.AutoCAD.DatabaseServices;
    3 using Autodesk.AutoCAD.EditorInput;
    4 using Autodesk.AutoCAD.GraphicsInterface;
    5 using Autodesk.AutoCAD.GraphicsSystem;
    6 using Autodesk.AutoCAD.Runtime;
    7 using Autodesk.AutoCAD.Interop;
    8 using System.Drawing;
    9
   10 namespace OffscreenImageCreation
   11 {
   12   public class Commands
   13   {
   14     [CommandMethod("OSS")]
   15     static public void OffscreenSnapshot()
   16     {
   17       CreateSphere();
   18       SnapshotToFile(
   19         "c:\\sphere-Wireframe2D.png",
   20         VisualStyleType.Wireframe2D
   21       );
   22       SnapshotToFile(
   23         "c:\\sphere-Hidden.png",
   24         VisualStyleType.Hidden
   25       );
   26       SnapshotToFile(
   27         "c:\\sphere-Basic.png",
   28         VisualStyleType.Basic
   29       );
   30       SnapshotToFile(
   31         "c:\\sphere-ColorChange.png",
   32         VisualStyleType.ColorChange
   33       );
   34       SnapshotToFile(
   35         "c:\\sphere-Conceptual.png",
   36         VisualStyleType.Conceptual
   37       );
   38       SnapshotToFile(
   39         "c:\\sphere-Flat.png",
   40         VisualStyleType.Flat
   41       );
   42       SnapshotToFile(
   43         "c:\\sphere-Gouraud.png",
   44         VisualStyleType.Gouraud
   45       );
   46       SnapshotToFile(
   47         "c:\\sphere-Realistic.png",
   48         VisualStyleType.Realistic
   49       );
   50     }
   51
   52     static public void CreateSphere()
   53     {
   54       Document doc =
   55         Application.DocumentManager.MdiActiveDocument;
   56       Database db = doc.Database;
   57       Editor ed = doc.Editor;
   58
   59       Transaction tr =
   60         doc.TransactionManager.StartTransaction();
   61       using (tr)
   62       {
   63         BlockTable bt =
   64           (BlockTable)tr.GetObject(
   65             db.BlockTableId,
   66             OpenMode.ForRead
   67           );
   68         BlockTableRecord btr =
   69           (BlockTableRecord)tr.GetObject(
   70             bt[BlockTableRecord.ModelSpace],
   71             OpenMode.ForWrite
   72           );
   73         Solid3d sol = new Solid3d();
   74         sol.CreateSphere(10.0);
   75
   76         const string matname =
   77           "Sitework.Paving - Surfacing.Riverstone.Mortared";
   78         DBDictionary matdict =
   79           (DBDictionary)tr.GetObject(
   80             db.MaterialDictionaryId,
   81             OpenMode.ForRead
   82           );
   83         if (matdict.Contains(matname))
   84         {
   85           sol.Material = matname;
   86         }
   87         else
   88         {
   89           ed.WriteMessage(
   90             "\nMaterial (" + matname + ") not found" +
   91             " - sphere will be rendered without it.",
   92             matname
   93           );
   94         }
   95         btr.AppendEntity(sol);
   96
   97         tr.AddNewlyCreatedDBObject(sol, true);
   98         tr.Commit();
   99       }
  100       AcadApplication acadApp =
  101         (AcadApplication)Application.AcadApplication;
  102       acadApp.ZoomExtents();
  103     }
  104
  105     static public void SnapshotToFile(
  106       string filename,
  107       VisualStyleType vst
  108     )
  109     {
  110       Document doc =
  111         Application.DocumentManager.MdiActiveDocument;
  112       Editor ed = doc.Editor;
  113       Database db = doc.Database;
  114       Manager gsm = doc.GraphicsManager;
  115
  116       // Get some AutoCAD system variables
  117       int vpn =
  118         System.Convert.ToInt32(
  119           Application.GetSystemVariable("CVPORT")
  120         );
  121
  122       using (View view = new View())
  123       {
  124         gsm.SetViewFromViewport(view, vpn);
  125
  126         // Set the visual style to the one passed in
  127         view.VisualStyle = new VisualStyle(vst);
  128
  129         Device dev =
  130           gsm.CreateAutoCADOffScreenDevice();
  131         using (dev)
  132         {
  133           dev.OnSize(gsm.DisplaySize);
  134
  135           // Set the render type and the background color
  136           dev.DeviceRenderType = RendererType.Default;
  137           dev.BackgroundColor = Color.White;
  138
  139           // Add the view to the device and update it
  140           dev.Add(view);
  141           dev.Update();
  142
  143           using (Model model = gsm.CreateAutoCADModel())
  144           {
  145             Transaction tr =
  146               db.TransactionManager.StartTransaction();
  147             using (tr)
  148             {
  149               // Add the modelspace to the view
  150               // It's a container but also a drawable
  151               BlockTable bt =
  152                 (BlockTable)tr.GetObject(
  153                   db.BlockTableId,
  154                   OpenMode.ForRead
  155                 );
  156               BlockTableRecord btr =
  157                 (BlockTableRecord)tr.GetObject(
  158                   bt[BlockTableRecord.ModelSpace],
  159                   OpenMode.ForRead
  160                 );
  161               view.Add(btr, model);
  162               tr.Commit();
  163             }
  164             // Take the snapshot
  165             Rectangle rect = view.Viewport;
  166             using (Bitmap bitmap = view.GetSnapshot(rect))
  167             {
  168               bitmap.Save(filename);
  169               ed.WriteMessage(
  170                 "\nSnapshot image saved to: " +
  171                 filename
  172               );
  173               // Clean up
  174               view.EraseAll();
  175               dev.Erase(view);
  176             }
  177           }
  178         }
  179       }
  180     }
  181   }
  182 }
在国外网站KEAN WALMSLEY用c#写的,哪位用c#帮忙编译一下

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

14

主题

64

帖子

4

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
120
发表于 2013-11-18 22:00:00 | 显示全部楼层
我的水平有点看不懂哦
回复

使用道具 举报

0

主题

334

帖子

12

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
335
发表于 2013-11-30 11:47:00 | 显示全部楼层
以后估计能看懂。。。
回复

使用道具 举报

xgr

56

主题

302

帖子

8

银币

中流砥柱

Rank: 25

铜币
526
发表于 2013-12-3 21:39:00 | 显示全部楼层

运行成功,在C盘生成了图纸的png图片
  1. using System;
  2. using System.Drawing;
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.DatabaseServices;
  5. using Autodesk.AutoCAD.EditorInput;
  6. using Autodesk.AutoCAD.GraphicsInterface;
  7. using Autodesk.AutoCAD.GraphicsSystem;
  8. using Autodesk.AutoCAD.Interop;
  9. using Autodesk.AutoCAD.Runtime;
  10. namespace 截图工具
  11. {
  12.     public class Commands
  13.     {
  14.         [CommandMethod("OSS")]
  15.         public static void OffscreenSnapshot()
  16.         {
  17.             CreateSphere();
  18.             SnapshotToFile(
  19.                 "c:\\sphere-Wireframe2D.png",
  20.                 VisualStyleType.Wireframe2D
  21.                 );
  22.             SnapshotToFile(
  23.                 "c:\\sphere-Hidden.png",
  24.                 VisualStyleType.Hidden
  25.                 );
  26.             SnapshotToFile(
  27.                 "c:\\sphere-Basic.png",
  28.                 VisualStyleType.Basic
  29.                 );
  30.             SnapshotToFile(
  31.                 "c:\\sphere-ColorChange.png",
  32.                 VisualStyleType.ColorChange
  33.                 );
  34.             SnapshotToFile(
  35.                 "c:\\sphere-Conceptual.png",
  36.                 VisualStyleType.Conceptual
  37.                 );
  38.             SnapshotToFile(
  39.                 "c:\\sphere-Flat.png",
  40.                 VisualStyleType.Flat
  41.                 );
  42.             SnapshotToFile(
  43.                 "c:\\sphere-Gouraud.png",
  44.                 VisualStyleType.Gouraud
  45.                 );
  46.             SnapshotToFile(
  47.                 "c:\\sphere-Realistic.png",
  48.                 VisualStyleType.Realistic
  49.                 );
  50.         }
  51.         public static void CreateSphere()
  52.         {
  53.             Document doc =
  54.                 Application.DocumentManager.MdiActiveDocument;
  55.             Database db = doc.Database;
  56.             Editor ed = doc.Editor;
  57.             Transaction tr =
  58.                 doc.TransactionManager.StartTransaction();
  59.             using (tr)
  60.             {
  61.                 var bt =
  62.                     (BlockTable) tr.GetObject(
  63.                         db.BlockTableId,
  64.                         OpenMode.ForRead
  65.                         );
  66.                 var btr =
  67.                     (BlockTableRecord) tr.GetObject(
  68.                         bt[BlockTableRecord.ModelSpace],
  69.                         OpenMode.ForWrite
  70.                         );
  71.                 var sol = new Solid3d();
  72.                 sol.CreateSphere(10.0);
  73.                 const string matname =
  74.                     "Sitework.Paving - Surfacing.Riverstone.Mortared";
  75.                 var matdict =
  76.                     (DBDictionary) tr.GetObject(
  77.                         db.MaterialDictionaryId,
  78.                         OpenMode.ForRead
  79.                         );
  80.                 if (matdict.Contains(matname))
  81.                 {
  82.                     sol.Material = matname;
  83.                 }
  84.                 else
  85.                 {
  86.                     ed.WriteMessage(
  87.                         "\nMaterial (" + matname + ") not found" +
  88.                         " - sphere will be rendered without it.",
  89.                         matname
  90.                         );
  91.                 }
  92.                 btr.AppendEntity(sol);
  93.                 tr.AddNewlyCreatedDBObject(sol, true);
  94.                 tr.Commit();
  95.             }
  96.             var acadApp =
  97.                 (AcadApplication)Application.AcadApplication;
  98.             acadApp.ZoomExtents();
  99.         }
  100.         public static void SnapshotToFile(
  101.             string filename,
  102.             VisualStyleType vst
  103.             )
  104.         {
  105.             Document doc =
  106.                 Application.DocumentManager.MdiActiveDocument;
  107.             Editor ed = doc.Editor;
  108.             Database db = doc.Database;
  109.             Manager gsm = doc.GraphicsManager;
  110.             // Get some AutoCAD system variables
  111.             int vpn =
  112.                 Convert.ToInt32(
  113.                     Application.GetSystemVariable("CVPORT")
  114.                     );
  115.             using (var view = new View())
  116.             {
  117.                 gsm.SetViewFromViewport(view, vpn);
  118.                 // Set the visual style to the one passed in
  119.                 view.VisualStyle = new VisualStyle(vst);
  120.                 Device dev =
  121.                     gsm.CreateAutoCADOffScreenDevice();
  122.                 using (dev)
  123.                 {
  124.                     dev.OnSize(gsm.DisplaySize);
  125.                     // Set the render type and the background color
  126.                     dev.DeviceRenderType = RendererType.Default;
  127.                     dev.BackgroundColor = Color.White;
  128.                     // Add the view to the device and update it
  129.                     dev.Add(view);
  130.                     dev.Update();
  131.                     using (Model model = gsm.CreateAutoCADModel())
  132.                     {
  133.                         Transaction tr =
  134.                             db.TransactionManager.StartTransaction();
  135.                         using (tr)
  136.                         {
  137.                             // Add the modelspace to the view
  138.                             // It's a container but also a drawable
  139.                             var bt =
  140.                                 (BlockTable) tr.GetObject(
  141.                                     db.BlockTableId,
  142.                                     OpenMode.ForRead
  143.                                     );
  144.                             var btr =
  145.                                 (BlockTableRecord) tr.GetObject(
  146.                                     bt[BlockTableRecord.ModelSpace],
  147.                                     OpenMode.ForRead
  148.                                     );
  149.                             view.Add(btr, model);
  150.                             tr.Commit();
  151.                         }
  152.                         // Take the snapshot
  153.                         
  154.                         Rectangle rect = view.Viewport;
  155.                         
  156.                         using (Bitmap bitmap = view.GetSnapshot(rect))
  157.                         {
  158.                             bitmap.Save(filename);
  159.                             ed.WriteMessage(
  160.                                 "\nSnapshot image saved to: " +
  161.                                 filename
  162.                                 );
  163.                             // Clean up
  164.                             view.EraseAll();
  165.                             dev.Erase(view);
  166.                         }
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.     }
  172. }
注意添加引用。

30wh3lw02br.JPG

30wh3lw02br.JPG


回复

使用道具 举报

9

主题

24

帖子

5

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
60
发表于 2014-5-6 23:06:00 | 显示全部楼层
路过并表示强烈支持!
回复

使用道具 举报

1

主题

2

帖子

1

银币

初来乍到

Rank: 1

铜币
6
发表于 2014-10-17 14:45:00 | 显示全部楼层
在windows server 2003 上 view.GetSnapshot(rect) GDI+中发生一般性错误
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 19:53 , Processed in 0.699083 second(s), 67 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表