Each raster image will have a definition called “RasterImageDef” and will be stores inside the raster image Dictionary. A raster image reactor is stored in “RasterImageDef” for each image referred to it. So if you count the number of raster image reactor, then you can identify the number of raster images referring to same “RasterImageDef”. Refer below code.
[CommandMethod("ImageCount")]
public static void ImageCount()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ObjectId imageDict = RasterImageDef.GetImageDictionary(db);
if (imageDict == ObjectId.Null)
{
ed.WriteMessage("No images in the drawing.\n");
return;
}
using (Transaction Tx =
db.TransactionManager.StartTransaction())
{
DBDictionary ImageDic =
(DBDictionary)Tx.GetObject(imageDict,OpenMode.ForRead);
ObjectIdCollection ImageCount = new ObjectIdCollection();
foreach (DBDictionaryEntry ImageDef in ImageDic)
{
RasterImageDef imageDef = (RasterImageDef)Tx.GetObject(
ImageDef.Value, OpenMode.ForRead);
ObjectIdCollection ids =
imageDef.GetPersistentReactorIds();
foreach (ObjectId id in ids)
{
DBObject reactor =
Tx.GetObject(id, OpenMode.ForRead);
string name = reactor.GetRXClass().DxfName;
if (string.Compare(name,
"IMAGEDEF_REACTOR", true) == 0)
{
if (!reactor.OwnerId.IsErased)
{
DBObject obj = Tx.GetObject(reactor.OwnerId,
OpenMode.ForRead);
if (obj is RasterImage)
{
ImageCount.Add(obj.ObjectId);
}
}
}
}
if (ImageCount.Count > 0)
{
ed.WriteMessage("Raster image file " +
imageDef.ActiveFileName + " is used "
+ ImageCount.Count.ToString() + " times\n");
}
ImageCount.Clear();
}
Tx.Commit();
}
}