First I'm sorry for asking the same question in another forum.
~~~~~~~~~~~~~~~~~~
Usually, when inserting a raster image, we should assign its pathname in RasterImageDef, and
the image should exist as a file in the disk.
Here I wanna create a image in Memory instead of disk, and display it in AutoCAD. The image data
are stored in an array like this: byte[].
I guess I might use SetImage() method of RasterImageDef. But I don't know how to use it.
SetImage() method has 3 parameters to be assigned :
public virtual void SetImage(IntPtr image, IntPtr fileDescription, bool modifyDatabase);
Should I use this method to display an image in Memory? Then, how? Or any method else?
Here are codes I used to insert an image in the disk below. I think some lines must be changed to reach my current target.
Thanks in advance!
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database; Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager; using (Transaction t = tm.StartTransaction()) { // open the dictionary Autodesk.AutoCAD.DatabaseServices.ObjectId imgDictID = RasterImageDef.GetImageDictionary(db); DBDictionary imgDict; if (imgDictID.OldId == 0) { imgDictID = RasterImageDef.CreateImageDictionary(db); } // create the raster image definition RasterImageDef rasterDef = new RasterImageDef(); // rasterDef.SourceFileName = mapURL; string fName; Point3d pt; fName = "D:\\atoll.jpg"; rasterDef.SourceFileName = fName; rasterDef.Load(); // test the image dictionary and the raster before going further bool bTestLoad = rasterDef.IsLoaded; Autodesk.AutoCAD.DatabaseServices.ObjectId TestImgDictID = RasterImageDef.GetImageDictionary(db); //Write the dictionary imgDict = (DBDictionary)t.GetObject(imgDictID, OpenMode.ForWrite); // add the raster definition to the dictionary if it doesn't already exist Autodesk.AutoCAD.DatabaseServices.ObjectId rasterDefID; String name = "test"; if (!imgDict.Contains(name)) { rasterDefID = imgDict.SetAt(name, rasterDef); } else { Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("Image Already Exits!"); return; } t.AddNewlyCreatedDBObject(rasterDef, true); bool bTestImage = imgDict.Contains(name); // now add the REAL raster image reference RasterImage rasterImg = new RasterImage(); rasterImg.ImageDefId = rasterDef.ObjectId; pt = new Point3d(500, 400, 0); rasterImg.Orientation = new CoordinateSystem3d(pt, new Vector3d(2000, 0, 0), new Vector3d(0, 1500, 0)); BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false); BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false); btr.AppendEntity(rasterImg); tm.AddNewlyCreatedDBObject(rasterImg, true); testRefID = rasterImg.ObjectId; rasterImg.AssociateRasterDef(rasterDef); t.Commit(); t.Dispose(); }
Just a general comment... I don't believe there is any way to embed an image that is not a file. All AutoCAD raster images are basically xref'd files.
You may be able to draw an image on the AutoCAD graphics screen using a custom draw routine but I believe your application would be responsible for rendering the graphic and refreshing it.
You could probably go as far as creating reactors to refresh the image as the user zoom/pans/etc. Basically you would be creating a custom object to embed images in a drawing. Object enabler type stuff.
The ability to embed raster images and control the plotting color would be a very nice addition to AutoCAD rather than all these 3D tools they keep adding.