.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Convert RasterImage to OLE (i.e. Bind Image)

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
10992 Views, 4 Replies

Convert RasterImage to OLE (i.e. Bind Image)

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.

 

4 REPLIES 4
Message 2 of 5
moogalm
in reply to: Anonymous

We don't have OLE API, and is not a trivial task. May I know what is you end objective of this task, if it is to get rid of dependency of external file from the drawing then this blog should help you. http://adndevblog.typepad.com/autocad/2015/04/embedding-an-image-in-a-drawing.html
Message 3 of 5
Anonymous
in reply to: Anonymous

The rasteredge .net image converter trial can help you, its developer guide will tell you more details on it.

Message 4 of 5
Rockit_for_Revit
in reply to: Anonymous

Thank you sdarali,

I got this to work in .net. Except the new OLE image position and size does not match the original rasterImage position & size.

Have you ore anyone else had any luck with this and can share there code?

Kind Regards

David

 

 

Message 5 of 5
phuongnamlc92
in reply to: Anonymous

Did you have resolve the problem? i'm looking for answer too.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report