当我在Kean的博客上看到这个东西的时候,第一个使我感到好奇的地方是:怎么截取到屏幕上的图呢?快速浏览了一下代码后彻底醒悟了,原来.NET里面的Graphics对象就有个函数能简单实现——就是CopyFromScreen,它能复制选定屏幕范围的图像,然后图像被自动保存在Graphics对象最初设定的图片格式文件(比如位图)中。这么简单,真是汗言!其它的事情就很简单了,比如在AutoCAD中怎么获取用户选定的区域然后转换成屏幕坐标范围,这些用AutoCAD API就能实现。
下面是转载自Kean的博客:
Design iteration #2 of Screenshot, November's Plugin of the Month
Thanks to all those who provided feedback on this recent post. I really appreciate people taking the time to provide feedback – both positive and negative – as I want to ensure this tool is used by more than just a few AutoCAD users. Today's version extends the last one to include a few more "Settings":
-
Existing settings
-
Output
- Allows the user to choose whether the captured image is copied to the clipboard or saved to a raster file
-
Background
-
Allows the user to force the background colour to white
- Hopefully useful for people using a non-quite background but want to create images for documentation
-
-
-
New settings
-
Foreground
-
Allows the user to force the foreground colour (and that means everything not the same colour as the background, at least for now) to black
- This can be used in combination with "Background" to create a pure black & white image
-
-
Grayscale
- Allows the user to force the resultant image to be grayscale (can be used in conjunction with Foreground and/or Background, but may therefore not have any visible effects)
-
At Mark Johnston's suggestion (and thanks for your help with testing these options, Mark!) I also added a message to indicate when the capture has occurred (and where the resultant image has been placed). And thanks to Viru Aithal, our DevTech India Manager, for providing some code to change the background colour of a 3D view.
In terms of future enhancements… I'm chewing on an option to hide the navigations aids (e.g. the UCS Icon and the ViewCube). Visibility of the UCS Icon should be simple enough to control via the active viewport, but disabling the ViewCube is likely to be more complex: I haven't yet seen a .NET equivalent for the acdbGetShowCompass() ObjectARX function – P/Invoking it is possible, I expect – and the other option would involve calling the NAVVCUBE command, but as it needs to run synchronously I'd have to use SendCommand() via COM. Either approach adds complexity and/or version dependency issues, so I'm starting to veer away from this direction. Please do post any thoughts you might have as comments, of course.
Here's the updated C# code:
To get a feel for the command-line prompts, here's the code in action, with the user-inputs in red:
Command: SCREENSHOT
Current settings: Output=Clipboard, Background=Normal
Select first point of capture window or [Document/Application/Objects/Settings]: S
Current settings: Output=Clipboard, Background=Normal
Setting to change [Output/Background/Exit] <Exit>: O
Save to file or place on the clipboard [File/Clipboard] <Clipboard>: F
Current settings: Output=File, Background=Normal
Setting to change [Output/Background/Exit] <Exit>: B
Force background color to white [Yes/No] <No>: Y
Current settings: Output=File, Background=ForceToWhite
Setting to change [Output/Background/Exit] <Exit>:
Current settings: Output=File, Background=ForceToWhite
Select first point of capture window or [Document/Application/Objects/Settings]: D
Regenerating model.
Regenerating model.
Command:
If you get the chance, please give the code a try and let me know what more you'd like to see the SCREENSHOT command do.
Here are the results of running SCREENSHOT with various options, starting with a black drawing background (which is quite common-place):
Background = Normal
Foreground = Normal
Grayscale = Off
Background = ForceToWhite
Foreground = Normal
Grayscale = Off
Background = ForceToWhite
Foreground = ForceToBlack
Grayscale = On/Off
Background = ForceToWhite
Foreground = Normal
Grayscale = On
Background = Normal
Foreground = Normal
Grayscale = On
Background = Normal
Foreground = ForceToBlack
Grayscale = On/Off
The last one is clearly less than useful, but it's there for completeness. :-)
Just to show it works, here are a few images taken in a 3D view (shown in the same sequence as the above 2D images):
The clipping of the 3D window when using the "Document" option seems slightly different in 3D (as you can see from the additional line at the bottom of the screen), which is something I'll address, at some point.
Anyway, please do keep the feedback coming (although as I'm on vacation – and away from a PC - for the rest of the week you may not see me respond until next week).