重要的两个类:view plaincopy to clipboardprint?
public static Image GetThumbnail(string dwgFilePath)
{
if (!(File.Exists(dwgFilePath)))
{
return null;
}
FileStream dwgStream = null;
BinaryReader dwgReader = null;
MemoryStream bmpStream = new MemoryStream();
BinaryWriter bmpWriter = new BinaryWriter(bmpStream);
try
{
dwgStream = new FileStream(dwgFilePath, FileMode.Open, FileAccess.Read);
dwgReader = new BinaryReader(dwgStream);
dwgStream.Seek(13, SeekOrigin.Begin); //从第十三字节开始读取
int dwgDescPos = dwgReader.ReadInt32(); //第13到17字节指示缩略图描述块的位置
dwgStream.Seek(dwgDescPos + 30, SeekOrigin.Begin); //将指针移到缩略图描述块的第31字节
int typePreview = dwgReader.ReadByte(); //第31字节为缩略图格式信息,2 为BMP格式,3为WMF格式
if (typePreview == 2 || typePreview == 3)
{
int bihPos = dwgReader.ReadInt32(); //BIH
int bmpLen = dwgReader.ReadInt32(); //位图大小
dwgStream.Seek(bihPos + 14, SeekOrigin.Begin); //跳过BIH前14字节到biBitCount
if (dwgReader.ReadInt16() != 8) //biBitCount != 8
{
return null;
}
dwgStream.Seek(bihPos, SeekOrigin.Begin);
byte[] bmpData = dwgReader.ReadBytes(bmpLen); //包含在DWG文件中的BMP数据,从BIH开始
bmpWriter.Write((short)19778); //BFH.bfType : 'BM', 4bytes
bmpWriter.Write(0L); //BFH.bfSize, BFH.bfReserved : 0, 8bytes
bmpWriter.Write(1078); //BFH.bfOffBits : BFH + BIH + Color Table = 14 + 40 + 1024, 4bytes
bmpWriter.Write(SwapBlackAndWhiteInColorTable(bmpData));
bmpStream.Seek(0, SeekOrigin.Begin);
return Image.FromStream(bmpStream);
}
return null;
}
catch (Exception)
{
return null;
}
finally
{
if (dwgReader != null)
{
dwgReader.Close();
}
if (dwgStream != null)
{
dwgStream.Close();
}
bmpWriter.Close();
bmpStream.Close();
}
}
///
/// 黑白反色
///
/// Image描述内容的字节码
///
private static byte[] SwapBlackAndWhiteInColorTable(byte[] bytes)
{
for (int i = 40; i < 1064; i += 4)
{
if (bytes[i] == 0 && bytes[i + 1] == 0 && bytes[i + 2] == 0)
{
bytes[i] = bytes[i + 1] = bytes[i + 2] = 0xFF;
}
else if (bytes[i] == 0xFF && bytes[i + 1] == 0xFF && bytes[i + 2] == 0xFF)
{
bytes[i] = bytes[i + 1] = bytes[i + 2] = 0;
}
}
return bytes;
}
public static Image GetThumbnail(string dwgFilePath)
{
if (!(File.Exists(dwgFilePath)))
{
return null;
}
FileStream dwgStream = null;
BinaryReader dwgReader = null;
MemoryStream bmpStream = new MemoryStream();
BinaryWriter bmpWriter = new BinaryWriter(bmpStream);
try
{
dwgStream = new FileStream(dwgFilePath, FileMode.Open, FileAccess.Read);
dwgReader = new BinaryReader(dwgStream);
dwgStream.Seek(13, SeekOrigin.Begin); //从第十三字节开始读取
int dwgDescPos = dwgReader.ReadInt32(); //第13到17字节指示缩略图描述块的位置
dwgStream.Seek(dwgDescPos + 30, SeekOrigin.Begin); //将指针移到缩略图描述块的第31字节
int typePreview = dwgReader.ReadByte(); //第31字节为缩略图格式信息,2 为BMP格式,3为WMF格式
if (typePreview == 2 || typePreview == 3)
{
int bihPos = dwgReader.ReadInt32(); //BIH
int bmpLen = dwgReader.ReadInt32(); //位图大小
dwgStream.Seek(bihPos + 14, SeekOrigin.Begin); //跳过BIH前14字节到biBitCount
if (dwgReader.ReadInt16() != 8) //biBitCount != 8
{
return null;
}
dwgStream.Seek(bihPos, SeekOrigin.Begin);
byte[] bmpData = dwgReader.ReadBytes(bmpLen); //包含在DWG文件中的BMP数据,从BIH开始
bmpWriter.Write((short)19778); //BFH.bfType : 'BM', 4bytes
bmpWriter.Write(0L); //BFH.bfSize, BFH.bfReserved : 0, 8bytes
bmpWriter.Write(1078); //BFH.bfOffBits : BFH + BIH + Color Table = 14 + 40 + 1024, 4bytes
bmpWriter.Write(SwapBlackAndWhiteInColorTable(bmpData));
bmpStream.Seek(0, SeekOrigin.Begin);
return Image.FromStream(bmpStream);
}
return null;
}
catch (Exception)
{
return null;
}
finally
{
if (dwgReader != null)
{
dwgReader.Close();
}
if (dwgStream != null)
{
dwgStream.Close();
}
bmpWriter.Close();
bmpStream.Close();
}
}
///
/// 黑白反色
///
/// Image描述内容的字节码
///
private static byte[] SwapBlackAndWhiteInColorTable(byte[] bytes)
{
for (int i = 40; i < 1064; i += 4)
{
if (bytes[i] == 0 && bytes[i + 1] == 0 && bytes[i + 2] == 0)
{
bytes[i] = bytes[i + 1] = bytes[i + 2] = 0xFF;
}
else if (bytes[i] == 0xFF && bytes[i + 1] == 0xFF && bytes[i + 2] == 0xFF)
{
bytes[i] = bytes[i + 1] = bytes[i + 2] = 0;
}
}
return bytes;
}
,代码想写好的简单测试代码下载地址:http://www.objectarx.net/bbs/viewthread.php?tid=1502&highlight=%CB%F5%C2%D4%CD%BC!~
有做cad二次开发的大家来交流下心得....
0 评论:
发表评论