Message 1 of 8
Inserting Images Help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm using this code to insert images, but I'm getting an error.
Public Function Add_Raster(ByRef ThisDoc As Document, ByVal RasterFile As String, ByVal InsPoint As Point3d, ByVal Scale As Double, ByRef NumInserted As Integer) As RasterImage
Dim RasterEnt As RasterImage, RasterDef As RasterImageDef
Dim ImageDicID, ImageDefID As ObjectId, ImageDic As DBDictionary
Dim Matrix As Geometry.Matrix3d
Dim btr As BlockTableRecord
Dim RasterName As String = ""
RasterName = RasterFile.Substring(RasterFile.LastIndexOf("\") + 1)
RasterName = RasterName.Substring(0, RasterName.IndexOf("."))
Add_Raster = Nothing
If ThisDoc Is Nothing Then Exit Function
Try
Using Trans As Transaction = ThisDoc.TransactionManager.StartTransaction()
RasterEnt = New RasterImage
RasterEnt.Dispose() ' force loading of RasterImage.dbx module (needed for 2009 and earlier)
RasterDef = New RasterImageDef
RasterDef.SourceFileName = RasterFile
RasterDef.ActiveFileName = RasterFile
RasterDef.Load()
ImageDicID = RasterImageDef.GetImageDictionary(ThisDoc.Database)
If ImageDicID.IsNull Then
RasterImageDef.CreateImageDictionary(ThisDoc.Database)
ImageDicID = RasterImageDef.GetImageDictionary(ThisDoc.Database)
End If
If ImageDicID.IsNull Then
MsgBox("Could not create image dictionary", MsgBoxStyle.Critical, "Failed")
'Exit Try
End If
ImageDic = Trans.GetObject(ImageDicID, OpenMode.ForWrite)
If ImageDic Is Nothing Then
MsgBox("Could not open image dictionary", MsgBoxStyle.Critical, "Failed")
Else
If ImageDic.Contains(RasterName) Then
MsgBox("That image name is already in use", MsgBoxStyle.Critical, "Failed")
Else
ImageDic.UpgradeOpen()
ImageDefID = ImageDic.SetAt(RasterName, RasterDef)
Trans.AddNewlyCreatedDBObject(RasterDef, True)
RasterEnt = New RasterImage
RasterEnt.SetDatabaseDefaults(ThisDoc.Database)
RasterEnt.ColorIndex = 256
RasterEnt.Linetype = "ByLayer"
RasterEnt.LineWeight = LineWeight.ByLayer
'If IsSTB Then RasterEnt.PlotStyleName = "ByLayer"
Matrix = New Geometry.Matrix3d
Matrix = Geometry.Matrix3d.Scaling(Scale / RasterDef.Size.X, New Point3d(0, 0, 0))
RasterEnt.TransformBy(Matrix)
Matrix = Geometry.Matrix3d.Displacement(New Vector3d(InsPoint.X, InsPoint.Y, InsPoint.Z))
RasterEnt.TransformBy(Matrix)
RasterEnt.ImageDefId = ImageDefID
btr = Trans.GetObject(ThisDoc.Database.CurrentSpaceId, OpenMode.ForWrite)
btr.AppendEntity(RasterEnt)
Trans.AddNewlyCreatedDBObject(RasterEnt, True)
Trans.Commit()
NumInserted = NumInserted + 1
End If
End If
End Using
Add_Raster = RasterEnt
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.ToString & vbCr & ex.Message)
End Try
End Function
and this to call the function
<CommandMethod("img")> _
Public Sub img()
Dim myDB2 As Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim Point As New Point3d(0, 0, 0)
Add_Raster(myDB2, "c:\test.jpg", Point, 0, 1)
End SubI get the following error:
Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
at Autodesk.AutoCAD.DabaseServices.Entity.TransformBy(Matrix3d transform)
at Kin.CommonFunctions.Add_Raster(Document& ThisDoc, String RasterFile, Point3d InsPoint, Double Scale, Int32& NumInserted) in C:\{path to vb file}: line 406 eInvalidInput