Hi,
You can try this:
public static void AttachRasterImage(Database db, string fileName, Point3d position, double scale)
{
string imageName = SymbolUtilityServices.GetBlockNameFromInsertPathName(fileName);
using (var tr = db.TransactionManager.StartTransaction())
{
var imageDictId = RasterImageDef.GetImageDictionary(db);
if (imageDictId.IsNull)
{
imageDictId = RasterImageDef.CreateImageDictionary(db);
}
var imageDict = (DBDictionary)tr.GetObject(imageDictId, OpenMode.ForRead);
ObjectId imageDefId;
RasterImageDef imageDef;
if (imageDict.Contains(imageName))
{
imageDefId = imageDict.GetAt(imageName);
imageDef = (RasterImageDef)tr.GetObject(imageDefId, OpenMode.ForRead);
}
else
{
imageDef = new RasterImageDef() { SourceFileName = fileName };
imageDef.Load();
tr.GetObject(imageDictId, OpenMode.ForWrite);
imageDefId = imageDict.SetAt(imageName, imageDef);
tr.AddNewlyCreatedDBObject(imageDef, true);
}
var image = new RasterImage();
image.ImageDefId = imageDefId;
image.Orientation = new CoordinateSystem3d(
position,
new Vector3d(imageDef.ResolutionMMPerPixel.X * image.Width * scale, 0.0, 0.0),
new Vector3d(0.0, imageDef.ResolutionMMPerPixel.Y * image.Height * scale, 0.0)); ;
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
curSpace.AppendEntity(image);
tr.AddNewlyCreatedDBObject(image, true);
RasterImage.EnableReactors(true);
image.AssociateRasterDef(imageDef);
tr.Commit();
}
}