I think I used Tony's code as a starting point a while back. And after fiddling for a while, I came up with this function which you can call once for each image you want to insert.
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 Exception
MsgBox("Error inserting image " & RasterName, MsgBoxStyle.Critical, "Failed")
End Try
End Function