AutoCAD Map 3D Developer
Welcome to Autodesk’s AutoCAD Map 3D Developer Forums. Share your knowledge, ask questions, and explore popular AutoCAD Map 3D Developer topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Import Raster image ECW from VB.NET

6 REPLIES 6
Reply
Message 1 of 7
imago69_
1061 Views, 6 Replies

Import Raster image ECW from VB.NET

Hello,

 

I want to import a raster image (.ECW or .JPG) to my Autocad Map through a development in VB.NET.

 

I can do it with the command MAPIMPORT as user, but I don't know how to do it in a .NET application.

 

Thanks a lot

Tags (4)
6 REPLIES 6
Message 2 of 7

Message 3 of 7

Thanks Alberto,

 

That code doesn´t fail but it doesn´t show my image...

Message 4 of 7

Hello,

 

When you talk about "Map", it is usually very important to pay attention to the coordinates where the image is inserted.

 

If there is no error, it is likely that the image is being inserted in wrong location (try to zoom to all extension to see if the image is inserted nezr  0,0 coordinate).

 

Time ago, I had to insert ".ecw"-s, for that, I used to send the "MAPIIMPORT" command to the commandline. Could this be another option?

 

By the way, this is an extraction of the code I'm using to load images in some of my Apps (I'm sorry that the comments are in Spanish):

 

    ''' <summary>
    ''' Inserta el raster solicitado en el dibujo.
    ''' </summary>
    ''' <param name="FileName"></param>
    ''' <param name="DownLeft"></param>
    ''' <param name="UpRight"></param>
    ''' <remarks></remarks>
    ''' 
    Public Shared Sub InsertRaster(ByVal FileName As String, ByVal Transparent As Boolean, _
                             ByVal DownLeft As Point3d, ByVal UpRight As Point3d)

        Try

            ' Get the current document and database
            Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim docLock As DocumentLock = acDoc.LockDocument
            Dim acTrans As Transaction = acDoc.TransactionManager.StartTransaction()

            Using acTrans
                Dim idModel As ObjectId = SymbolUtilityServices.GetBlockModelSpaceId(acDoc.Database)
                Dim ms As BlockTableRecord = TryCast(acTrans.GetObject(idModel, OpenMode.ForWrite), BlockTableRecord)
                Dim imageDef As RasterImageDef = AddImageDef(acDoc.Database, acTrans, FileName) ', Height, Width)
                If imageDef IsNot Nothing Then
                    Dim image As New RasterImage()
                    image.ImageDefId = imageDef.ObjectId
                    Dim imageId As ObjectId = ms.AppendEntity(image)
                    If imageId.IsNull Then
                        Throw New Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NullObjectId)
                    End If

                    'Con esto se envía la imagen al fondo.
                    Dim dot As DrawOrderTable = DirectCast(acTrans.GetObject(ms.DrawOrderTableId, OpenMode.ForWrite), DrawOrderTable)
                    Dim ids As New ObjectIdCollection()
                    ids.Add(image.ObjectId)
                    dot.MoveToBottom(ids)

                    'Posicionamiento de la imagen.
                    image.Orientation = New CoordinateSystem3d(DownLeft, New Vector3d(UpRight.X - DownLeft.X, 0, 0), New Vector3d(0, UpRight.Y - DownLeft.Y, 0))
                    'Asociación de la imagen con la definición cargada.
                    image.AssociateRasterDef(imageDef)
                    'La imagen se introducirá en al capa "0"
                    image.Layer = "0"
                    'Si se requiere, se indica la transparencia de la imagen.
                    If Transparent Then image.DisplayOptions = ImageDisplayOptions.Transparent

                    'Finalmente se agrega la imagen al dibujo.
                    acTrans.AddNewlyCreatedDBObject(image, True)

                End If
                imageDef.Dispose()
                acTrans.Commit()
            End Using
            acDoc.Editor.UpdateScreen()
            docLock.Dispose()
            acTrans.Dispose()

        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try

    End Sub

    ''' <summary>
    ''' Agrega una definición de raster al diccionario.
    ''' </summary>
    ''' <param name="db"></param>
    ''' <param name="trans"></param>
    ''' <param name="imageFilespec"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function AddImageDef(ByVal db As Database, ByVal trans As Transaction, ByVal imageFilespec As String) ', ByVal Height As Double, ByVal Width As Double) As RasterImageDef
        If Not System.IO.File.Exists(imageFilespec) Then
            Throw New System.IO.FileNotFoundException(imageFilespec)
        End If
        Dim idImageDict As ObjectId = RasterImageDef.GetImageDictionary(db)
        If idImageDict.IsNull Then
            idImageDict = RasterImageDef.CreateImageDictionary(db)
        End If
        If idImageDict.IsNull Then
            Throw New InvalidOperationException("failed to get or create image dictionary")
        End If

        Dim imageDictionary As DBDictionary = TryCast(trans.GetObject(idImageDict, OpenMode.ForRead), DBDictionary)
        If imageDictionary Is Nothing Then
            Throw New InvalidOperationException("Failed to open image dictionary")
        End If

        Dim imageDef As New RasterImageDef()
        imageDef.SourceFileName = imageFilespec
        imageDef.ResolutionUnits = Unit.Meter
        imageDef.Load()

        Dim name As String = ConfigLoader.Config.AppName & "_" & RasterImageDef.SuggestName(imageDictionary, imageFilespec)

        imageDictionary.UpgradeOpen()
        Dim idEntry As ObjectId = imageDictionary.SetAt(name, imageDef)
        imageDictionary.DowngradeOpen()

        If idEntry.IsNull Then
            imageDef.Dispose()
            imageDef = Nothing
            Throw New InvalidOperationException("Failed to add image definition to image dictionary")
        End If

        trans.AddNewlyCreatedDBObject(imageDef, True)

        Return imageDef
    End Function

 

Luis Alberto Manero, Geograma.com
Message 5 of 7

Thanks Alberto. This code loads my image, but I don't know which position (Point3D: DownLeft and UpRight) I have to pass to the merhod InsertRaster.

 

It is supposed that a ECW file is positionated automatically in its coordenates?

Message 6 of 7

Hello imago69_,

 

I'm sorry, but when I've inserted ECWs, I had their coordinates in a database to get the points, so I haven't had that problem...

 

I will try to make some tests to see if I can help you.

 

Best regards.

 

 

Luis Alberto Manero, Geograma.com
Message 7 of 7

Ok Alberto,

 

I think that I can obtain the coordinates of those points, so I will try and tell you the result.

 

Thanks a lot!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost