.NET

.NET

Reply
Active Contributor
btmcad
Posts: 31
Registered: ‎05-09-2007
Message 1 of 6 (1,765 Views)

create a bitmap file from the current dwg

1765 Views, 5 Replies
12-03-2012 11:10 AM

I'm working on a VB.NET program that resizes the current drawing window and creates a bitmap file.

Currently, I can get the window to resize to the correct length and width. However, I'm lost as far as creating a bitmap. I found some code in C# on the Through the Interface site:

http://through-the-interface.typepad.com/through_the_interface/2009/09/taking-screenshots-of-autocad...

 

Unfortunately, I'm not that familiar with C# and can't get the code to work if I try to convert to VB.net.

Any help would be greatly appreciated.

 

Ultimately, I want to then convert the bmp file to a gif with a transparent background. I know how to convert a bmp to gif, but is it possible to programmatically convert it to a transparent background?

 

Thanks

btm

Contributor ow
Contributor
ow
Posts: 18
Registered: ‎01-17-2009
Message 2 of 6 (1,746 Views)

Re: create a bitmap file from the current dwg

12-03-2012 02:10 PM in reply to: btmcad

You can use the BMPOUT command. (Or PNGOUT).

 

 

Regards,

ow

Distinguished Mentor
gasty1001
Posts: 582
Registered: ‎04-11-2010
Message 3 of 6 (1,727 Views)

Re: create a bitmap file from the current dwg

12-03-2012 06:43 PM in reply to: btmcad

Hi,

 

You can use an online service to translate from C# to VB, like this DevFusion, using that, the code from Kean blog translate to this:

 

Imports acApp = Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports System.Drawing.Imaging
Imports System.Drawing

Namespace ScreenshotTest
	Public Class Commands
		<CommandMethod("CSS")> _
		Public Shared Sub CaptureScreenShot()
			ScreenShotToFile(acApp.Application.MainWindow, "c:\main-window.png", 0, 0, 0, 0)
			ScreenShotToFile(acApp.Application.DocumentManager.MdiActiveDocument.Window, "c:\doc-window.png", 30, 26, 10, 10)
		End Sub

		Private Shared Sub ScreenShotToFile(wd As Autodesk.AutoCAD.Windows.Window, filename As String, top As Integer, bottom As Integer, left As Integer, right As Integer)
			Dim pt As Point = wd.Location
			Dim sz As Size = wd.Size

			pt.X += left
			pt.Y += top
			sz.Height -= top + bottom
			sz.Width -= left + right

			' Set the bitmap object to the size of the screen

			Dim bmp As New Bitmap(sz.Width, sz.Height, PixelFormat.Format32bppArgb)
			Using bmp
				' Create a graphics object from the bitmap

				Using gfx As Graphics = Graphics.FromImage(bmp)
					' Take a screenshot of our window

					gfx.CopyFromScreen(pt.X, pt.Y, 0, 0, sz, CopyPixelOperation.SourceCopy)

					' Save the screenshot to the specified location

					bmp.Save(filename, ImageFormat.Png)
				End Using
			End Using
		End Sub
	End Class
End Namespace

 

As for a GIF file with a transparency color, you can do it using GDI+ and setting the Alfa of the selected color to 0 in the palette of the GIF, remember that GIF file have a palette with the colors (max 256) to display.

 

Gaston Nunez

 

 

 

 

 

Active Contributor
btmcad
Posts: 31
Registered: ‎05-09-2007
Message 4 of 6 (1,711 Views)

Re: create a bitmap file from the current dwg

12-03-2012 08:12 PM in reply to: gasty1001

Thank you Gaston,

I didn't explain clearly in my original post, but the bmp will be created from a selection set. I'm not sure how I can use that code with a selection set.

Here's my code. I commented where I need to create the bmp. This can be done with COM, however, I would prefer to figure out how to do it with .net code.

 

 Public Sub Main()

            Dim doc As Document = Application.DocumentManager.MdiActiveDocument()
            Dim ed As Editor = doc.Editor
            Dim docWindow As Window = doc.Window

            docWindow.WindowState = Window.State.Normal

            Dim size As System.Drawing.Size = docWindow.GetSize

            ed.WriteMessage(vbCrLf + "Document Size:" + vbCrLf + size.ToString() + vbCrLf)

            Dim newSize As System.Drawing.Size = New System.Drawing.Size(950, 573) 'was 608

            docWindow.SetSize(newSize)

            ZoomExtents()

            '' Select objects in the drawing area
            Dim ssPrompt As PromptSelectionResult = ed.GetSelection()

            '' If the prompt status is OK, objects were selected
            If ssPrompt.Status = PromptStatus.OK Then
                Dim acSSet As SelectionSet = ssPrompt.Value

                

                'This is where I want to export the selection set to a bmp file

            End If

 

Distinguished Mentor
gasty1001
Posts: 582
Registered: ‎04-11-2010
Message 5 of 6 (1,707 Views)

Re: create a bitmap file from the current dwg

12-03-2012 09:52 PM in reply to: btmcad

Hi,

 

What about of using the plotting API to export the zoomed area to a bitmap file?, you have to use the appropriate pc3 configuration file (Publish to Web JPG/PNG.pc3  or create a new one in the plotter manager).

 

Gaston Nunez

Contributor ow
Contributor
ow
Posts: 18
Registered: ‎01-17-2009
Message 6 of 6 (1,698 Views)

Re: create a bitmap file from the current dwg

12-03-2012 10:56 PM in reply to: btmcad

The BMPOUT command does accept a selection set, but you would probably need to pInvoke acedCommand from objectArx c++ in order initialize it properly. (I'm using it from C++).

 

Otherwise, Keans' code should work just fine, just set visibility false or turn off unwanted layers on whatever you don't want to display in the image, and restore it afterwards.

 

ow

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.