Public Function GetSnapshot(Blockname As String) As Bitmap
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim gsm As Manager = doc.GraphicsManager
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim MyBitmap As New System.Drawing.Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height, Imaging.PixelFormat.Format32bppPArgb)
MyBitmap.SetResolution(200, 200)
' Get some AutoCAD system variables
Dim vpn As Integer = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"))
' Get AutoCAD's GS view for this document...
Dim gsv As View = doc.GraphicsManager.GetGsView(vpn, True)
' ... but create a new one for the actual snapshot
Using view As New View()
' Set the view to be just like the one
' in the AutoCAD editor
view.Viewport = gsv.Viewport
view.SetView(gsv.Position, gsv.Target, gsv.UpVector, gsv.FieldWidth, gsv.FieldHeight)
view.EraseAll()
' Set the visual style to the one passed in
view.VisualStyle = New VisualStyle(VisualStyleType.Wireframe2D)
Dim dev As Device = gsm.CreateAutoCADOffScreenDevice()
Using dev
dev.OnSize(gsm.DisplaySize)
' Set the render type and the background color
dev.DeviceRenderType = RendererType.[Default]
dev.BackgroundColor = Color.Black
' Add the view to the device and update it
dev.Add(view)
dev.Update()
Using model As Model = gsm.CreateAutoCADModel()
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Dim BlkRefID As ObjectId
Using tr
' Add the modelspace to the view
' It's a container but also a drawable
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
''Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt("R-12000-7250 A U is 0"), OpenMode.ForRead), BlockTableRecord)
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead), BlockTableRecord)
If bt.Has(Blockname) Then
''Dim dummy As ObjectId = InsertBlock(db, "ModelSpace", New Point3d(0, 0, 0), Blockname, 1, 1, 1, 0)
BlkRefID = InsertBlock(db, "ModelSpace", New Point3d(0, 0, 0), Blockname, 1, 1, 1, 0)
End If
view.Add(btr, model)
tr.Commit()
End Using
''Dim acadApp As Object = Application.AcadApplication
''acadApp.ZoomExtents(New Point3d(0, 0, 0), New Point3d(7500, 13000, 0))
view.ZoomExtents(New Point3d(0, 0, 0), New Point3d(7500, 13000, 0))
' Take the snapshot
Dim rect As Rectangle = view.Viewport
Using bitmap As Bitmap = view.GetSnapshot(rect)
MyBitmap = bitmap
bitmap.Save("c:\temp\" & Blockname & ".png")
''MyBitmap = bitmap
''bitmap.Save(filename)
''ed.WriteMessage(vbLf & "Snapshot image saved to: " & filename)
' Clean up
''view.EraseAll()
dev.[Erase](view)
''Return bitmap
End Using
Dim Mytr As Transaction = db.TransactionManager.StartTransaction()
Using Mytr
Dim bt As BlockTable = DirectCast(Mytr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
Dim btr As BlockTableRecord = DirectCast(Mytr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
Dim MyBlockRef As AcadDb.BlockReference = Mytr.GetObject(BlkRefID, OpenMode.ForWrite)
MyBlockRef.Erase()
Mytr.Commit()
End Using
End Using
End Using
view.Dispose()
End Using
Return MyBitmap
End Function