To add a raster image to a database and edit it, use the OdDbRasterImageDef and OdDbRasterImage classes. The OdDbRasterImageDef class works with the source image and image dictionaries. The OdDbRasterImage class works with the image as a database entity.
To add an image to the database:
1. Create a new or open an existing image dictionary.
To do so, use the createImageDictionary() method of the odDbRasterImageDef class, which requires one parameter to specify the current OdDbDatabase. For example:
OdDbObjectId imageDictId = OdDbRasterImageDef::createImageDictionary(pDb);
OdDbDictionaryPtr pImageDict = imageDictId.safeOpenObject(OdDb::kForWrite);
Note: If the image dictionary already exists in the database, its ID will be returned.
2. Create an ImageDef object.
Create an instance of the OdDbRasterImageDef class and add its entry to the image dictionary. To do so, use the setAt() method of the OdDbDictionary object, which returns the object ID of the newly added entry and requires two parameters: an OdString value as the name of the entry and a pointer to the database object to be added. For example:
OdDbRasterImageDefPtr pImageDef = OdDbRasterImageDef::createObject();
OdDbObjectId imageDefId = pImageDict->setAt(OD_T("OdRasterImageEx"), pImageDef);
3. Set the source image data.
To set the source image to the ImageDef object, use the following methods of the OdDbRasterImageDef class: setSourceFileName() + setImage() or setSourceFileName() + image(). The setSourceFileName() method sets the name of the raster image. It requires one OdString value to specify the name of the image file. The setImage() method creates an image from the specified OdGiRasterImage object. This method is used in applications that want to save a .dwg file without rendering if the width and height are known. It requires one pointer to the OdGiRasterImage object. The image() method is used in applications that need to render raster images immediately or export them to other formats. For example:
// Without rendering
pImageDef->setSourceFileName(OD_T("test_raster.jpg"));
pImageDef->setImage(OdGiRasterImageDesc::createObject(500, 500, OdGiRasterImage::kInch));
// With rendering
pImageDef->setSourceFileName(OD_T("test_raster.jpg"));
pImageDef->image(); // width, height will be loaded from actual raster file
4. Create a raster image entity and set the ImageDefId.
Simply create an instance of the raster image object and apply it in the database. Then link the ImageDefId of the entry from the image dictionary to the raster image object. For example:
OdDbRasterImagePtr pImage = OdDbRasterImage::createObject();
pImage->setDatabaseDefaults(pDb);
pModelSpace->appendOdDbEntity(pImage);
pImage->setImageDefId(imageDefId);
5. Set other parameters of the image.
Check and edit the properties of the raster image. For details, see Specific Raster Image Properties.
Copyright © 2002 – 2021. Open Design Alliance. All rights reserved.
|