I'm trying to create a BINDRASTER command using .Net in order to bind images in AutoCAD, since such a command is not available. To accomplish this, I wrote a small program to identify the Raster Image information, load it from a file into ClipBoard and paste into AutoCAD in the same location and size, then delete the original Raster Image.
There are two problems:
1. I cannot calculate the scale of the pasted image since it is in pixels only, no AutoCAD units.
2. If there are several images on the Layout Sheet, all the pasted images have the first calculated size even though the original images have different sizes.
3. For some reason, the images are pasted into the Model space instead of the Paper space.
Here's the code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; [assembly: CommandClass(typeof(AcadBindRaster.Class1))] namespace AcadBindRaster { public class Class1 { [CommandMethod("BindRaster")] public void BindRaster() { // Get the current document and database, and start a transaction Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; LayoutManager lm = LayoutManager.Current; //Iterate through the layouts string imagetype = "AcDbRasterImage"; Transaction trans = db.TransactionManager.StartTransaction(); DBDictionary layObjects = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary; foreach (DBDictionaryEntry layObject in layObjects) { lm.CurrentLayout = layObject.Key; Layout layout = trans.GetObject(lm.GetLayoutId(layObject.Key), OpenMode.ForRead) as Layout; BlockTableRecord blktbl = trans.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord; //ObjectId imageDictId = RasterImageDef.GetImageDictionary(db); foreach (ObjectId imageId in blktbl) { //Filter the raster image objects if (imageId.ObjectClass.Name == imagetype) { DBObject obj = trans.GetObject(imageId, OpenMode.ForWrite); RasterImage raster = obj as RasterImage; RasterImageDef rasterDef = trans.GetObject(raster.ImageDefId, OpenMode.ForWrite) as RasterImageDef; string filename = rasterDef.ActiveFileName; Point3d position = new Point3d(raster.Position.X - raster.Width, raster.Position.Y - raster.Height, 0); double pixelwidth= 0; double pixelheight = 0; if (db.Measurement == MeasurementValue.English) { ed.WriteMessage("Found English/Imperial units."); pixelwidth = raster.ImageWidth / 96 * raster.Width * 25.4; pixelheight = raster.ImageHeight / 96 * raster.Height * 25.4; } else { ed.WriteMessage("Found Metric units."); pixelwidth = raster.ImageWidth / 96 * raster.Width; pixelheight = raster.ImageHeight / 96 * raster.Height; } PasteImage(doc, filename, pixelwidth, pixelheight, position.X, position.Y); raster.Erase(); rasterDef.Erase(); } } } trans.Commit(); } /// <summary> /// Paste the image in the AutoCAD document /// </summary> /// <param name="acDoc">AutoCAD document</param> /// <param name="filename">Full path for the XREF image</param> /// <param name="width">Width in pixels</param> /// <param name="height">Height in pixels</param> /// <param name="x">Position in x</param> /// <param name="y">Position in y</param> private void PasteImage(Document acDoc, string filename, double width, double height, double x, double y) { System.Drawing.Image image = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(filename); //Set the size of the loaded image with the size of the XREF image image = ResizeImage(image, new System.Drawing.Size((int)width, (int)height)); //Copy the image into the clipboard System.Windows.Forms.Clipboard.Clear(); System.Windows.Forms.Clipboard.SetImage(image); //Paste the image using the Autocad PASTECLIP command //Since we cannot create an OLE object in API acDoc.SendStringToExecute("._pasteclip " + x + "," + y + " ", true, false, false); } /// <summary> /// Change the size of an image /// </summary> /// <param name="imgToResize">Image to resize</param> /// <param name="size">The size in pixels</param> private static System.Drawing.Image ResizeImage(System.Drawing.Image imgToResize, System.Drawing.Size size) { return (System.Drawing.Image)(new System.Drawing.Bitmap(imgToResize, size)); } } }
Appreciate any assistance on what is going on wrong in this example.
The rasteredge .net image converter trial can help you, its developer guide will tell you more details on it.
Can't find what you're looking for? Ask the community or share your knowledge.