How to add a raster object?
DWG and DXF specification provides the Image
entity that represents a raster object in the drawing.
CAD .NET allows adding raster images. You can find the corresponding demo in the AddEntities
project included into the CAD .NET demo package.
In this example we will add a raster image to the Entities.dxf
file. You can find it in CAD .NET 14\Files
.
By default, the CAD .NET folder is unpacked to Documents.
- Add the
using
directive with theCADImport
andCADImport.FaceModule
namespaces.
using CADImport;
using CADImport.FaceModule;
More information about CADPictureBox
The CADPictureBox
class is the basic implementation of the control element for displaying vector drawings. Visually CADPictureBox
includes only area for drawing visualization and can be extended by the required control elements in the project under development.
To get more information about the CAD .NET controls, see What controls does CAD .NET have?
- Use the control element of the
CADPictureBox
class:- Set the
Location
property asnew Point(12, 41)
. - Set the
BackColor
property asColor.Black
. - Set the
Size
property asnew Size(776, 620)
. - Finally, add it to the form.
- Set the
...
CADPictureBox pictureBox1 = new CADPictureBox(){
Location = new Point(10, 30),
BackColor = Color.Black,
Size = new Size(995, 500),
}
public Form1()
{
Controls.Add(pictureBox1);
InitializeComponent();
}
- Add a new button. Name it
AddRasterImage
. Then create theAddRasterImage_Click
function to add a raster object to a CAD drawing by click.
private void AddRasterImage_Click(object sender, EventArgs e){
- Create a new instance of the
CADImage
class. Also, change theBackgroundColor
property of thisCADImage
instance.
CADImage vDrawing = CADImage.CreateImageByExtension(@"Entities.dxf");
vDrawing.LoadFromFile(@"Entities.dxf");
vDrawing.BackgroundColor = Color.Azure;
- Declare the local string variable
fileName
. In this example we use thelogo.png
raster image.
string sFileName = "logo.png";
- Create a new instance of the
CADImageDef
class. Set theFileName
property as thesFileName
variable.
CADImageDef vImageDef = new CADImageDef();
vImageDef.FileName = sFileName;
- Add
vImageDef
to the current layout ofvDrawing
using theAddEntity
method. Use theLoads
method to fill the internal data of the entity to prepare it for drawing.
if (new Bitmap(vImageDef.FileName) != null)
{
vDrawing.CurrentLayout.AddEntity(vImageDef);
vDrawing.Converter.Loads(vImageDef);
- Create a new instance of the
CADImageEnt
class. Set its properties:- Add this entity to the current layout of
vDrawing
using theAddEntity
method. - Set the
ImageDef
property asvImageDef
. - Set the
Point
property withDPoint
. - Set the
UVector
property asCADConst.XOrtAxis
. - Set the
VVector
property asCADConst.YOrtAxis
. - Set the
Size
property withDPoint
. - Use the
Loads
method to fill the internal data of the entity to prepare it for drawing.
- Add this entity to the current layout of
CADImageEnt vImageEnt = new CADImageEnt();
vDrawing.CurrentLayout.AddEntity(vImageEnt);
vImageEnt.ImageDef = vImageDef;
vImageEnt.Point = new DPoint(30, 14, 0);
vImageEnt.UVector = CADConst.XOrtAxis;
vImageEnt.VVector = CADConst.YOrtAxis;
vImageEnt.Size = new DPoint(43,23.23, 0);
vDrawing.Converter.Loads(vImageEnt);
- Use the
GetExtents
method to recalculate drawing extents.
vDrawing.GetExtents();
- Declare the local variable
vRect
and specifyRectangleF
as its type. This variable stores four floating values that represent the location and size of a CAD file. Use the following code to fit the CAD file topictureBox1
. Finally, render the drawing with theDraw
method.
RectangleF vRect;
double vRatio = (double)(vDrawing.AbsHeight * pictureBox1.ClientSize.Width) / (vDrawing.AbsWidth * pictureBox1.ClientSize.Height);
if (vRatio > 1)
vRect = new RectangleF(0, 0, (float)(pictureBox1.ClientSize.Width / vRatio), (float)pictureBox1.ClientSize.Height);
else
vRect = new RectangleF(0, 0, (float)pictureBox1.ClientSize.Width, (float)(pictureBox1.ClientSize.Height * vRatio));
vDrawing.Draw(pictureBox1.CreateGraphics(), vRect);
}
The following picture illustrates the result.
You have created the function to add a raster image to CAD files.
The full code listing.
...
using CADImport;
using CADImport.FaceModule;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
CADPictureBox pictureBox1 = new CADPictureBox()
{
Location = new Point(12, 41),
TabIndex = 10,
BackColor = Color.Black,
Size = new Size(776, 620)
};
public Form1()
{
Controls.Add(pictureBox1);
InitializeComponent();
}
private void AddRasterImage_Click(object sender, EventArgs e)
{
CADImage vDrawing = CADImage.CreateImageByExtension(@"Entities.dxf");
vDrawing.LoadFromFile(@"Entities.dxf");
vDrawing.BackgroundColor = Color.Azure;
vDrawing.CurrentLayout = vDrawing.Layouts[0];
string sFileName = "logo.png";
CADImageDef vImageDef = new CADImageDef();
vImageDef.FileName = sFileName;
if (new Bitmap(vImageDef.FileName) != null)
{
vDrawing.CurrentLayout.AddEntity(vImageDef);
vDrawing.Converter.Loads(vImageDef);
CADImageEnt vImageEnt = new CADImageEnt();
vDrawing.CurrentLayout.AddEntity(vImageEnt);
vImageEnt.ImageDef = vImageDef;
vImageEnt.Point = new DPoint(30, 14, 0);
vImageEnt.UVector = CADConst.XOrtAxis;
vImageEnt.VVector = CADConst.YOrtAxis;
vImageEnt.Size = new DPoint(43,23.23, 0);
vDrawing.Converter.Loads(vImageEnt);
vDrawing.GetExtents();
RectangleF vRect;
double vRatio = (double)(vDrawing.AbsHeight * pictureBox1.ClientSize.Width) / (vDrawing.AbsWidth * pictureBox1.ClientSize.Height);
if (vRatio > 1)
vRect = new RectangleF(0, 0, (float)(pictureBox1.ClientSize.Width / vRatio), (float)pictureBox1.ClientSize.Height);
else
vRect = new RectangleF(0, 0, (float)pictureBox1.ClientSize.Width, (float)(pictureBox1.ClientSize.Height * vRatio));
vDrawing.Draw(pictureBox1.CreateGraphics(), vRect);
}
}
}
}