embed raster images in dwg file

hossam.amerUY9KR
Participant
Participant

embed raster images in dwg file

hossam.amerUY9KR
Participant
Participant

Hi guys, I'm trying embedding raster images programmatically in dwg files by copying the images then pasting them as the manual way to do it. the problem here is that the quality is so bad because I'm copying the image directedly not by editing it in Paint them copy it from there. So what I need now is a way to copy the image as it's being copied from Paint without opening paint or using it at all by doing some image processing or using any free open source library.
Any ideas?

0 Likes
Reply
209 Views
2 Replies
Replies (2)

_gile
Mentor
Mentor

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();
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes

hossam.amerUY9KR
Participant
Participant

the dwg file already has the raster Images and I'm trying to convert the to OLE objects by copying and pasting so I can later delete the raster images 

0 Likes