VB.NET Import Image

VB.NET Import Image

Sgear
Advocate Advocate
1,347 Views
1 Reply
Message 1 of 2

VB.NET Import Image

Sgear
Advocate
Advocate

 

 

Hi

 

I am insert Image I export from Arcmap into Autocad Map with this sample

need to set the Width and Height and Scale  and right please in the map

if I use .NET the image are not on same X Y if I use .NET or Autocad to Import the Image

 

 

this is X Y I get with Export from arcmap in the .TFW file

Dim X = "353917.0678034420"

Dim Y = "398701.9004743111"

 

if I import Image with Autocad then this is the X Y

X = '353917.018
Y = '398423.450

 

 

http://adndevblog.typepad.com/autocad/2012/05/how-to-insert-a-rasterimage-using-the-net-api.html

 

 

       <CommandMethod("INSERTIMAGE")> _
        Public Sub CmdInsertImage()
            ' Image dictionary name constant
            Dim dictName As String = "IMAGE"
            Dim imageDef As RasterImageDef
            Dim imageDefId As ObjectId

            Dim db As Database =
              Application.DocumentManager.MdiActiveDocument.Database
            Dim ed As Editor =
              Application.DocumentManager.MdiActiveDocument.Editor
            Using trans As Transaction = db.TransactionManager.StartTransaction
                Try
                    Dim imageDictId As ObjectId =
                      RasterImageDef.GetImageDictionary(db)

                    If imageDictId.IsNull Then
                        ' Image dictionary doesn't exist, create new
                        imageDictId = RasterImageDef.CreateImageDictionary(db)
                    End If

                    ' Open the image dictionary
                    Dim imageDict As DBDictionary = trans.GetObject( _
                      imageDictId, OpenMode.ForRead)

                    ' See if our raster def in the image dict exists,
                    ' if not, create new
                    If imageDict.Contains(dictName) Then
                        ' Get the raster image def
                        imageDefId = imageDict.GetAt(dictName)
                        imageDef = trans.GetObject(imageDefId, OpenMode.ForWrite)
                    Else
                        ' Create a raster image definition
                        ' Get the file name of the image
                        Dim fileDia As New Autodesk.AutoCAD.Windows.OpenFileDialog(
                          "Open an Image file", Nothing,
                          "jpg; gif; tif; bmp", "Image File",
                          Autodesk.AutoCAD.Windows.
                                OpenFileDialog.OpenFileDialogFlags.NoUrls)

                        ' Show the dialog and test the result
                        Dim res As System.Windows.Forms.DialogResult =
                          fileDia.ShowDialog()
                        If res <> System.Windows.Forms.DialogResult.OK Then Return

                        ' Create a new image definition
                        imageDef = New RasterImageDef()
                        ' And set its source image
                        imageDef.SourceFileName = fileDia.Filename()
                        ' finally load it
                        imageDef.Load()
                        imageDict.UpgradeOpen()
                        imageDefId = imageDict.SetAt(dictName, imageDef)
                        trans.AddNewlyCreatedDBObject(imageDef, True)
                    End If

                    ' Now create the raster image that references the definition
                    Dim image As New RasterImage()
                    image.ImageDefId = imageDefId

                    Dim X = "353917.0678034420"
                    Dim Y = "398701.9004743111"

                    Dim uCorner As New Vector3d(366, 0, 0)
                    Dim vOnPlane As New Vector3d(0, 278, 0)
                    Dim point As New Point3d(X, Y, 0.0)

                    Dim coordinateSystem As New CoordinateSystem3d(point, uCorner, vOnPlane)
                    image.Orientation = coordinateSystem

                    ' And some other properties
                    image.ImageWidth.ToString(366)
                    image.ImageHeight.ToString(278)
                    image.ImageTransparency = True
                    image.ShowImage = True

                    ' Add the image to ModelSpace
                    Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
                    Dim msBtr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                    msBtr.AppendEntity(image)
                    trans.AddNewlyCreatedDBObject(image, True)
                    RasterImage.EnableReactors(True)
                    image.AssociateRasterDef(imageDef)

                    trans.Commit()
                Catch
                End Try
            End Using
        End Sub
0 Likes
1,348 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable

hi, 

tfw file also stores scale/resolution and rotations of image,

that means distances on picture by pixels needs to get converted to meters or other units.

i've just read your code but haven't tested it, your base point (upper left corner) seems good but i believe the error is in that scale factor

When georeferencing images you should pay attention to this parameters, base point, scale and rotations.

The same idea is with height and width of picture, it is in pixels not meters.

 

I could write more about it, if you need more help let me know, i suspect on image pixels width and height vs. real units.

Cheers, enjoy in your day!

 

0 Likes