几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量

几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 (http://www.dimcax.com/hust/index.php)
-   ObjectARX(VB.NET/C#) (http://www.dimcax.com/hust/forumdisplay.php?f=176)
-   -   【转帖】完美解决窗体中预览dwg图形(c#) - 精华帖集合 (http://www.dimcax.com/hust/showthread.php?t=14142)

yang686526 2009-04-29 05:30 PM

【转帖】完美解决窗体中预览dwg图形(c#) - 精华帖集合
 
完美解决窗体中预览dwg图形(c#) - 精华帖集合
www.dimcax.com
完美解决窗体中预览dwg图形(c#)
原文在mjtd 上, vb.net 写的,下面是c#代码
class viewdwg
{
struct bitmapfileheader
{
public short bftype;
public int bfsize;
public short bfreserved1;
public short bfreserved2;
public int bfoffbits;
}
public static image getdwgimage(string filename)
{
if (!(file.exists(filename)))
{
throw new filenotfoundexception("文件没有被找到");
}
filestream dwgf; //文件流
int possentinel; //文件描述块的位置
binaryreader br; //读取二进制文件
int typepreview; //缩略图格式
int posbmp; //缩略图位置
int lenbmp; //缩略图大小
short bibitcount; //缩略图比特深度
bitmapfileheader bih; //bmp文件头,dwg文件中不包含位图文件头,要自行加上去
byte[] bmpinfo; //包含在dwg文件中的bmp文件体
memorystream bmpf = new memorystream(); //保存位图的内存文件流
binarywriter bmpr = new binarywriter(bmpf); //写二进制文件类
image myimg = null;
try
{
dwgf = new filestream(filename, filemode.open, fileaccess.read); //文件流
br = new binaryreader(dwgf);
dwgf.seek(13, seekorigin.begin); //从第十三字节开始读取
possentinel = br.readint32(); //第13到17字节指示缩略图描述块的位置
dwgf.seek(possentinel + 30, seekorigin.begin); //将指针移到缩略图描述块的第31字节
typepreview = br.readbyte(); //第31字节为缩略图格式信息,2 为bmp格式,3为wmf格式
if (typepreview == 1)
{
}
else if (typepreview == 2 || typepreview == 3)
{
posbmp = br.readint32(); //dwg文件保存的位图所在位置
lenbmp = br.readint32(); //位图的大小
dwgf.seek(posbmp + 14, seekorigin.begin); //移动指针到位图块
bibitcount = br.readint16(); //读取比特深度
dwgf.seek(posbmp, seekorigin.begin); //从位图块开始处读取全部位图内容备用
bmpinfo = br.readbytes(lenbmp); //不包含文件头的位图信息
br.close();
dwgf.close();
bih.bftype = 19778; //建立位图文件头
if (bibitcount < 9)
{
bih.bfsize = 54 + 4 * (int)(math.pow(2, bibitcount)) + lenbmp;
}
else
{
bih.bfsize = 54 + lenbmp;
}
bih.bfreserved1 = 0; //保留字节
bih.bfreserved2 = 0; //保留字节
bih.bfoffbits = 14 + 40 + 1024; //图像数据偏移
//以下开始写入位图文件头
bmpr.write(bih.bftype); //文件类型
bmpr.write(bih.bfsize); //文件大小
bmpr.write(bih.bfreserved1); //0
bmpr.write(bih.bfreserved2); //0
bmpr.write(bih.bfoffbits); //图像数据偏移
bmpr.write(bmpinfo); //写入位图
bmpf.seek(0, seekorigin.begin); //指针移到文件开始处
myimg = image.fromstream(bmpf); //创建位图文件对象
bmpr.close();
bmpf.close();
}
return myimg;
}
catch(exception ex)
{
throw new exception(ex.message);
}
}
}
复制代码
在哪儿显示
问一下 图象在什么地方显示 myimg = image.fromstream(bmpf); 有错误呀没有fromstream 方法
picturebox1.image = getdwgimage("c\\test.dwg")
好东西,楼主辛苦
这个可以读取dwg2005的图形信息吗?也就是bmp信息;
原帖由 yinzhiw 于 2006-8-28 08:16 am 发表 picturebox1.image = getdwgimage("c\\test.dwg")
估计你用的不是system.drawing 中的image
完美是完美,但不能判断版本。我打开2008文件时,程序崩溃。
请问怎么调整预览大小,还有就是背景颜色了?
学习
学问必须合乎自己的兴趣,方才可以得益。


所有的时间均为北京时间。 现在的时间是 04:19 PM.