About Attaching and Scaling a Raster Image (VBA/ActiveX)

Images can be placed in a drawing file, but they are not actually part of the file. The image is linked to the drawing file through a path name or a data management document ID.

Linked image paths can be changed or removed at any time. To attach an image, you create a Raster object in your drawing using the AddRaster method. This method takes four values as input: the name of the image file to attach, the insertion point in the drawing to place the image, the scale factor of the image, and the rotation angle of the image. Remember, the Raster object represents an independent link to the image, not the image itself.

Once you have attached an image, you can reattach it many times, creating a new Raster object for each attachment. Each attachment has its own clip boundary and its own settings for brightness, contrast, fade, and transparency. A single image can be cut into multiple pieces and rearranged independently in your drawing.

You can set the raster image scale factor when you create the Raster object so that the image's geometry scale matches the scale of the geometry created in the AutoCAD drawing. When you select an image to attach, the image is inserted at a scale factor of 1 image unit of measurement to 1 AutoCAD unit of measurement. To set the image scale factor, you need to know the scale of the geometry on the image, and you need to know what unit of measurement (inches, feet, and so forth) you want to use to define 1 AutoCAD unit. The image file must contain resolution information defining the DPI, or dots per inch, and number of pixels in the image.

If an image has resolution information, AutoCAD combines it with the scale factor and the AutoCAD unit of measurement you supply to scale the image in your drawing. For example, if your raster image is a scanned blueprint on which the scale is 1 inch equals 50 feet, or 1:600, and your AutoCAD drawing is set up so that 1 unit represents 1 inch, then to set the scale factor of the image, you enter 600 for the ScaleFactor parameter of the AddRaster method. AutoCAD then inserts the image at a scale that brings the geometry in the image into alignment with the vector geometry in the drawing.

Note: If no resolution information is defined with the attached image file, AutoCAD calculates the image's original width as one unit. After insertion, the image width in AutoCAD units is equal to the scale factor.

Attach a Raster Image

This example adds a raster image in model space. This example uses a file named watch.jpg. Replace the name and location of the image assigned to the imageName variable in the code with one that is available on your workstation.

Sub Ch10_AttachingARaster()
  Dim insertionPoint(0 To 2) As Double
  Dim scalefactor As Double
  Dim rotationAngle As Double
  Dim imageName As String
  Dim rasterObj As AcadRasterImage
  imageName = "C:/Program Files/AutoCAD Directory/sample/watch.jpg'
  insertionPoint(0) = 5
  insertionPoint(1) = 5
  insertionPoint(2) = 0
  scalefactor = 2
  rotationAngle = 0

  On Error GoTo ERRORHANDLER
  ' Attach the raster image in model space
  Set rasterObj = ThisDrawing.ModelSpace.AddRaster(imageName, insertionPoint, scalefactor, rotationAngle)
  ZoomAll
  Exit Sub

ERRORHANDLER:
  MsgBox Err.Description
End Sub