VectorDraw Developer Framework常见问题整理(七十):如何将图像映射到绘图单位

VectorDraw Developer Framework(VDF)是一个用于应用程序可视化的图形引擎库。有了VDF提供的功能,您可以轻松地创建、编辑、管理、输出、输入和打印2D和3D图形文件。该库还支持许多矢量和栅格输入和输出格式,包括本地PDF和SVG导出。

下载VectorDraw Developer Framework(VDF)最新版

VectorDraw Framework使用vdPolyFace或vdGroundSurface对象的MappedImages属性导出了将图像映射到这些对象的能力。这方面的例子可以在我们的AddEntities示例中找到。
如果你想在你的绘图上映射一个单一的图像,你可以使用下面的示例代码。

假设我们有一个名称为 "myimage.jpg "的图像
同时我们有2个像素坐标系中的点,相对于图像的左上角

例子
    P1_pixel_x , P1_pixel_y

    P2_pixel_x , P2_pixel_y

现在,我们有了一个要映射图像的图纸

vdDocument doc;//already defined VectorDraw Document object。
//为选中的像素点
构造gPoints gPoint imgp1 = new gPoint(P1_pixel_x , P1_pixel_y,0);
gPoint imgp2 = new gPoint(P2_pixel_x, P2_pixel_y,0);

//将imagedefinition添加到绘图中
vdImageDef imagedef = doc.Images.Add("myimage.jpg" );

double Aspect =(double) imagedef .Height / (double)imagedef .Width;//width,所选图像的高度(以像素为单位)

//创建一个矩阵,将图像的像素坐标转换为 Unint 宽度矩阵
矩阵 pixeltoworld = new Matrix();
pixeltoworld.ScaleMatrix(1.0 / magedef .Width, -Aspect /imagedef .Height, 1.0d);
pixeltoworld.TranslateMatrix(0.0, Aspect, 0.0);
//将像素坐标转换为单位世界坐标
imgp1 = pixeltoworld.Transform(imgp1);
imgp2 = pixeltoworld.Transform(imgp2);

vector offsrc = new Vector(imgp2 - imgp1);//源向量

Vector offdst =new Vector(P2_world -P1_world );//目标向量

double scale = offdst.Length / offsrc.Length;

//计算映射图像的最终变换矩阵
Matrix m2 = new Matrix();
m2.TranslateMatrix(imgp1 * -1);
m2.ScaleMatrix(scale, scale, 1.0);
m2.RotateZMatrix(offdst.Angle2DDirection() - offsrc.Angle2DDirection());
m2.TranslateMatrix(P1_world);

//创建一个新的图像引用所选的“myimage.jpg”
vdImage img2 = new vdImage(doc);
img2.ImageDefinition = imagedef ;
img2.PenColor = new vdColor(Color.White, 100);//使图像透明

img2.Transformby(m2);//将图像变换到绘图中的正确位置

doc.Model.Entities.AddItem(img2); //将图像添加到模型绘图实体

doc.Redraw(true);//强制重绘绘图

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值