怎样脱离autocad情况下预览dwg?
VB
Public Class acThumbnailReader
Private Structure BITMAPINFOHEADER
Dim biSize As Integer
Dim biWidth As Integer
Dim biHeight As Integer
Dim biPlanes As Short
Dim biBitCount As Short
Dim biCompression As Integer
Dim biSizeImage As Integer
Dim biXPelsPerMeter As Integer
Dim biYPelsPerMeter As Integer
Dim biClrUsed As Integer
Dim biClrImportant As Integer
End Structure
Private Structure RGBQUAD
Dim rgbBlue As Byte
Dim rgbGreen As Byte
Dim rgbRed As Byte
Dim rgbReserved As Byte
End Structure
Private Structure IMGREC
Dim bytType As Byte
Dim lngStart As Integer
Dim lngLen As Integer
End Structure
Public Shared Function GetThumbnail(ByVal strFile As String, Optional ByVal boolRetainBackColor As Boolean = True, Optional ByVal boolSaveToFile As Boolean = False, Optional ByVal strSaveName As String = "") As Bitmap
Dim bmp As New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
Dim bytCnt As Byte
Dim bytBMPBuff() As Byte
Dim lngImgLoc As Integer
Dim lngFile As Integer
Dim lngCurLoc As Integer
Dim lngY As Integer
Dim lngX As Integer
Dim lngColor As Integer
Dim lngCnt As Integer
Dim intCnt As Short
Dim udtRec As IMGREC
Dim udtColors() As RGBQUAD
Dim udtColor As RGBQUAD
Dim udtHeader As BITMAPINFOHEADER
Dim intRed As Short
Dim intGreen As Short
Dim intBlue As Short
On Error GoTo Err_Control
If Len(Dir(strFile)) > 0 Then
lngFile = FreeFile()
FileOpen(lngFile, strFile, OpenMode.Binary)
Seek(lngFile, 14)
FileGet(lngFile, lngImgLoc)
Seek(lngFile, lngImgLoc + 17)
lngCurLoc = Seek(lngFile)
Seek(lngFile, lngCurLoc + 4)
FileGet(lngFile, bytCnt)
If bytCnt > 1 Then
For intCnt = 1 To bytCnt
FileGet(lngFile, udtRec)
If udtRec.bytType = 2 Then
Seek(lngFile, udtRec.lngStart + 1)
FileGet(lngFile, udtHeader)
ReDim bytBMPBuff(udtRec.lngLen)
If udtHeader.biBitCount = 8 Then
ReDim udtColors(256)
FileGet(lngFile, udtColors)
Seek(lngFile, udtRec.lngStart)
FileGet(lngFile, bytBMPBuff)
bmp = New Bitmap(udtHeader.biWidth, udtHeader.biHeight)
For lngY = 1 To udtHeader.biHeight
For lngX = udtHeader.biWidth To 1 Step -1
lngColor = bytBMPBuff(UBound(bytBMPBuff) - lngCnt)
udtColor = udtColors(lngColor)
intRed = CShort(udtColor.rgbRed)
intGreen = CShort(udtColor.rgbGreen)
intBlue = CShort(udtColor.rgbBlue)
lngColor = RGB(intRed, intGreen, intBlue)
If boolRetainBackColor = False Then
If lngColor = Drawing.ColorTranslator.ToOle(Drawing.Color.Black) Then
lngColor = Drawing.ColorTranslator.ToOle(Drawing.Color.White)
ElseIf lngColor = Drawing.ColorTranslator.ToOle(Drawing.Color.White) Then
lngColor = Drawing.ColorTranslator.ToOle(Drawing.Color.Black)
End If
End If
bmp.SetPixel(lngX - 1, lngY - 1, Drawing.ColorTranslator.FromOle(lngColor))
lngCnt = lngCnt + 1
Next lngX
Next lngY
End If
Exit For
ElseIf udtRec.bytType = 3 Then
Exit For
End If
Next intCnt
End If
End If
Exit_Here:
FileClose(lngFile)
If boolSaveToFile = True Then
If (strSaveName = "") Then
Dim fName As String
fName = IO.Path.GetTempPath & IO.Path.GetFileNameWithoutExtension(IO.Path.GetRandomFileName) & ".bmp"
bmp.Save(fName)
Else
bmp.Save(strSaveName)
End If
End If
Return bmp
Exit Function
Err_Control:
Select Case Err.Number
Case Else
MsgBox(Err.Description)
Resume Exit_Here
End Select
End Function
End Class
Private Structure BITMAPINFOHEADER
Dim biSize As Integer
Dim biWidth As Integer
Dim biHeight As Integer
Dim biPlanes As Short
Dim biBitCount As Short
Dim biCompression As Integer
Dim biSizeImage As Integer
Dim biXPelsPerMeter As Integer
Dim biYPelsPerMeter As Integer
Dim biClrUsed As Integer
Dim biClrImportant As Integer
End Structure
Private Structure RGBQUAD
Dim rgbBlue As Byte
Dim rgbGreen As Byte
Dim rgbRed As Byte
Dim rgbReserved As Byte
End Structure
Private Structure IMGREC
Dim bytType As Byte
Dim lngStart As Integer
Dim lngLen As Integer
End Structure
Public Shared Function GetThumbnail(ByVal strFile As String, Optional ByVal boolRetainBackColor As Boolean = True, Optional ByVal boolSaveToFile As Boolean = False, Optional ByVal strSaveName As String = "") As Bitmap
Dim bmp As New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
Dim bytCnt As Byte
Dim bytBMPBuff() As Byte
Dim lngImgLoc As Integer
Dim lngFile As Integer
Dim lngCurLoc As Integer
Dim lngY As Integer
Dim lngX As Integer
Dim lngColor As Integer
Dim lngCnt As Integer
Dim intCnt As Short
Dim udtRec As IMGREC
Dim udtColors() As RGBQUAD
Dim udtColor As RGBQUAD
Dim udtHeader As BITMAPINFOHEADER
Dim intRed As Short
Dim intGreen As Short
Dim intBlue As Short
On Error GoTo Err_Control
If Len(Dir(strFile)) > 0 Then
lngFile = FreeFile()
FileOpen(lngFile, strFile, OpenMode.Binary)
Seek(lngFile, 14)
FileGet(lngFile, lngImgLoc)
Seek(lngFile, lngImgLoc + 17)
lngCurLoc = Seek(lngFile)
Seek(lngFile, lngCurLoc + 4)
FileGet(lngFile, bytCnt)
If bytCnt > 1 Then
For intCnt = 1 To bytCnt
FileGet(lngFile, udtRec)
If udtRec.bytType = 2 Then
Seek(lngFile, udtRec.lngStart + 1)
FileGet(lngFile, udtHeader)
ReDim bytBMPBuff(udtRec.lngLen)
If udtHeader.biBitCount = 8 Then
ReDim udtColors(256)
FileGet(lngFile, udtColors)
Seek(lngFile, udtRec.lngStart)
FileGet(lngFile, bytBMPBuff)
bmp = New Bitmap(udtHeader.biWidth, udtHeader.biHeight)
For lngY = 1 To udtHeader.biHeight
For lngX = udtHeader.biWidth To 1 Step -1
lngColor = bytBMPBuff(UBound(bytBMPBuff) - lngCnt)
udtColor = udtColors(lngColor)
intRed = CShort(udtColor.rgbRed)
intGreen = CShort(udtColor.rgbGreen)
intBlue = CShort(udtColor.rgbBlue)
lngColor = RGB(intRed, intGreen, intBlue)
If boolRetainBackColor = False Then
If lngColor = Drawing.ColorTranslator.ToOle(Drawing.Color.Black) Then
lngColor = Drawing.ColorTranslator.ToOle(Drawing.Color.White)
ElseIf lngColor = Drawing.ColorTranslator.ToOle(Drawing.Color.White) Then
lngColor = Drawing.ColorTranslator.ToOle(Drawing.Color.Black)
End If
End If
bmp.SetPixel(lngX - 1, lngY - 1, Drawing.ColorTranslator.FromOle(lngColor))
lngCnt = lngCnt + 1
Next lngX
Next lngY
End If
Exit For
ElseIf udtRec.bytType = 3 Then
Exit For
End If
Next intCnt
End If
End If
Exit_Here:
FileClose(lngFile)
If boolSaveToFile = True Then
If (strSaveName = "") Then
Dim fName As String
fName = IO.Path.GetTempPath & IO.Path.GetFileNameWithoutExtension(IO.Path.GetRandomFileName) & ".bmp"
bmp.Save(fName)
Else
bmp.Save(strSaveName)
End If
End If
Return bmp
Exit Function
Err_Control:
Select Case Err.Number
Case Else
MsgBox(Err.Description)
Resume Exit_Here
End Select
End Function
End Class
C#
//Copyright 2009 K.E. Blackie
//This work is based in part from a VBA macro
//initially developed by members of www.vbdesign.net
//it may be used freely and may be modified to suit
//your needs as long as credit is given where it is due
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
class acThumbnailReader
{
[StructLayout(LayoutKind.Sequential)]
private struct BITMAPINFOHEADER
{
public int biSize;
public int biWidth;
public int biHeight;
public short biPlanes;
public short biBitCount;
public int biCompression;
public int biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public int biClrUsed;
public int biClrImportant;
}
[StructLayout(LayoutKind.Sequential)]
private struct IMGREC
{
public byte bytType;
public int lngStart;
public int lngLen;
}
[StructLayout(LayoutKind.Sequential)]
private struct RGBQUAD
{
public byte rgbBlue;
public byte rgbGreen;
public byte rgbRed;
public byte rgbReserved;
}
public static Bitmap GetThumbnail(string strFile)
{
return GetThumbnail(strFile, true, false, "");
}
public static Bitmap GetThumbnail(string strFile, bool boolRetainBackColor)
{
return GetThumbnail(strFile, boolRetainBackColor, false, "");
}
public static Bitmap GetThumbnail(string strFile, bool boolRetainBackColor, bool boolSaveToFile)
{
return GetThumbnail(strFile, boolRetainBackColor, boolSaveToFile, "");
}
public static Bitmap GetThumbnail(string strFile, bool boolRetainBackColor, bool boolSaveToFile, string strSaveName)
{
Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);
byte bytCnt;
byte[] bytBMPBuff;
int lngImgLoc;
FileStream fs = null;
BinaryReader br = null;
int lngCurLoc;
int lngY;
int lngX;
int lngColor;
int lngCnt;
short intCnt;
IMGREC udtRec;
RGBQUAD[] udtColors;
RGBQUAD udtColor;
BITMAPINFOHEADER udtHeader;
short intRed;
short intGreen;
short intBlue;
try
{
if (File.Exists(strFile))
{
fs = File.OpenRead(strFile);
br = new BinaryReader(fs);
fs.Seek(13, SeekOrigin.Begin);
lngImgLoc = br.ReadInt32();
fs.Seek(lngImgLoc + 17, SeekOrigin.Begin);
lngCurLoc = lngImgLoc + 17;
fs.Seek(lngCurLoc + 3, SeekOrigin.Begin);
bytCnt = br.ReadByte();
if (bytCnt > 1)
{
for (intCnt = 0; intCnt < bytCnt; intCnt++)
{
udtRec.bytType = br.ReadByte();
udtRec.lngStart = br.ReadInt32();
udtRec.lngLen = br.ReadInt32();
if (udtRec.bytType == 2)
{
fs.Seek(udtRec.lngStart, SeekOrigin.Begin);
udtHeader.biSize = br.ReadInt32();
udtHeader.biWidth = br.ReadInt32();
udtHeader.biHeight = br.ReadInt32();
udtHeader.biPlanes = br.ReadInt16();
udtHeader.biBitCount = br.ReadInt16();
udtHeader.biCompression = br.ReadInt32();
udtHeader.biSizeImage = br.ReadInt32();
udtHeader.biXPelsPerMeter = br.ReadInt32();
udtHeader.biYPelsPerMeter = br.ReadInt32();
udtHeader.biClrUsed = br.ReadInt32();
udtHeader.biClrImportant = br.ReadInt32();
bytBMPBuff = new byte[udtRec.lngLen + 1];
if (udtHeader.biBitCount == 8)
{
udtColors = new RGBQUAD[256];
for (int count = 0; count < 256; count++)
{
udtColors[count].rgbBlue = br.ReadByte();
udtColors[count].rgbGreen = br.ReadByte();
udtColors[count].rgbRed = br.ReadByte();
udtColors[count].rgbReserved = br.ReadByte();
}
fs.Seek(udtRec.lngStart - 1, SeekOrigin.Begin);
for (int count = 0; count <= udtRec.lngLen; count++)
{
bytBMPBuff[count] = br.ReadByte();
}
bmp = new Bitmap(udtHeader.biWidth, udtHeader.biHeight);
lngCnt = 0;
for (lngY = 1; lngY <= udtHeader.biHeight; lngY++)
{
for (lngX = udtHeader.biWidth; lngX >= 1; lngX--)
{
lngColor = bytBMPBuff[bytBMPBuff.GetUpperBound(0) - lngCnt];
udtColor = udtColors[lngColor];
intRed = Convert.ToInt16(udtColor.rgbRed);
intGreen = Convert.ToInt16(udtColor.rgbGreen);
intBlue = Convert.ToInt16(udtColor.rgbBlue);
lngColor = ColorTranslator.ToOle(Color.FromArgb(intRed, intGreen, intBlue));
if (boolRetainBackColor == false)
{
if (lngColor == ColorTranslator.ToOle(Color.Black))
{
lngColor = ColorTranslator.ToOle(Color.White);
}
else
{
if (lngColor == ColorTranslator.ToOle(Color.White))
{
lngColor = ColorTranslator.ToOle(Color.Black);
}
}
}
bmp.SetPixel(lngX - 1, lngY - 1, ColorTranslator.FromOle(lngColor));
lngCnt++;
}
}
}
goto Exit_Here;
}
else
{
if (udtRec.bytType == 3)
{
goto Exit_Here;
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
goto Exit_Here;
}
Exit_Here:
if (br != null)
{
br.Close();
fs.Close();
fs.Dispose();
}
if (boolSaveToFile == true)
{
if (strSaveName == "")
{
string fName;
fName = String.Concat(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()), ".bmp");
bmp.Save(fName);
}
else
{
bmp.Save(strSaveName);
}
}
return bmp;
}
}
//This work is based in part from a VBA macro
//initially developed by members of www.vbdesign.net
//it may be used freely and may be modified to suit
//your needs as long as credit is given where it is due
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
class acThumbnailReader
{
[StructLayout(LayoutKind.Sequential)]
private struct BITMAPINFOHEADER
{
public int biSize;
public int biWidth;
public int biHeight;
public short biPlanes;
public short biBitCount;
public int biCompression;
public int biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public int biClrUsed;
public int biClrImportant;
}
[StructLayout(LayoutKind.Sequential)]
private struct IMGREC
{
public byte bytType;
public int lngStart;
public int lngLen;
}
[StructLayout(LayoutKind.Sequential)]
private struct RGBQUAD
{
public byte rgbBlue;
public byte rgbGreen;
public byte rgbRed;
public byte rgbReserved;
}
public static Bitmap GetThumbnail(string strFile)
{
return GetThumbnail(strFile, true, false, "");
}
public static Bitmap GetThumbnail(string strFile, bool boolRetainBackColor)
{
return GetThumbnail(strFile, boolRetainBackColor, false, "");
}
public static Bitmap GetThumbnail(string strFile, bool boolRetainBackColor, bool boolSaveToFile)
{
return GetThumbnail(strFile, boolRetainBackColor, boolSaveToFile, "");
}
public static Bitmap GetThumbnail(string strFile, bool boolRetainBackColor, bool boolSaveToFile, string strSaveName)
{
Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);
byte bytCnt;
byte[] bytBMPBuff;
int lngImgLoc;
FileStream fs = null;
BinaryReader br = null;
int lngCurLoc;
int lngY;
int lngX;
int lngColor;
int lngCnt;
short intCnt;
IMGREC udtRec;
RGBQUAD[] udtColors;
RGBQUAD udtColor;
BITMAPINFOHEADER udtHeader;
short intRed;
short intGreen;
short intBlue;
try
{
if (File.Exists(strFile))
{
fs = File.OpenRead(strFile);
br = new BinaryReader(fs);
fs.Seek(13, SeekOrigin.Begin);
lngImgLoc = br.ReadInt32();
fs.Seek(lngImgLoc + 17, SeekOrigin.Begin);
lngCurLoc = lngImgLoc + 17;
fs.Seek(lngCurLoc + 3, SeekOrigin.Begin);
bytCnt = br.ReadByte();
if (bytCnt > 1)
{
for (intCnt = 0; intCnt < bytCnt; intCnt++)
{
udtRec.bytType = br.ReadByte();
udtRec.lngStart = br.ReadInt32();
udtRec.lngLen = br.ReadInt32();
if (udtRec.bytType == 2)
{
fs.Seek(udtRec.lngStart, SeekOrigin.Begin);
udtHeader.biSize = br.ReadInt32();
udtHeader.biWidth = br.ReadInt32();
udtHeader.biHeight = br.ReadInt32();
udtHeader.biPlanes = br.ReadInt16();
udtHeader.biBitCount = br.ReadInt16();
udtHeader.biCompression = br.ReadInt32();
udtHeader.biSizeImage = br.ReadInt32();
udtHeader.biXPelsPerMeter = br.ReadInt32();
udtHeader.biYPelsPerMeter = br.ReadInt32();
udtHeader.biClrUsed = br.ReadInt32();
udtHeader.biClrImportant = br.ReadInt32();
bytBMPBuff = new byte[udtRec.lngLen + 1];
if (udtHeader.biBitCount == 8)
{
udtColors = new RGBQUAD[256];
for (int count = 0; count < 256; count++)
{
udtColors[count].rgbBlue = br.ReadByte();
udtColors[count].rgbGreen = br.ReadByte();
udtColors[count].rgbRed = br.ReadByte();
udtColors[count].rgbReserved = br.ReadByte();
}
fs.Seek(udtRec.lngStart - 1, SeekOrigin.Begin);
for (int count = 0; count <= udtRec.lngLen; count++)
{
bytBMPBuff[count] = br.ReadByte();
}
bmp = new Bitmap(udtHeader.biWidth, udtHeader.biHeight);
lngCnt = 0;
for (lngY = 1; lngY <= udtHeader.biHeight; lngY++)
{
for (lngX = udtHeader.biWidth; lngX >= 1; lngX--)
{
lngColor = bytBMPBuff[bytBMPBuff.GetUpperBound(0) - lngCnt];
udtColor = udtColors[lngColor];
intRed = Convert.ToInt16(udtColor.rgbRed);
intGreen = Convert.ToInt16(udtColor.rgbGreen);
intBlue = Convert.ToInt16(udtColor.rgbBlue);
lngColor = ColorTranslator.ToOle(Color.FromArgb(intRed, intGreen, intBlue));
if (boolRetainBackColor == false)
{
if (lngColor == ColorTranslator.ToOle(Color.Black))
{
lngColor = ColorTranslator.ToOle(Color.White);
}
else
{
if (lngColor == ColorTranslator.ToOle(Color.White))
{
lngColor = ColorTranslator.ToOle(Color.Black);
}
}
}
bmp.SetPixel(lngX - 1, lngY - 1, ColorTranslator.FromOle(lngColor));
lngCnt++;
}
}
}
goto Exit_Here;
}
else
{
if (udtRec.bytType == 3)
{
goto Exit_Here;
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
goto Exit_Here;
}
Exit_Here:
if (br != null)
{
br.Close();
fs.Close();
fs.Dispose();
}
if (boolSaveToFile == true)
{
if (strSaveName == "")
{
string fName;
fName = String.Concat(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()), ".bmp");
bmp.Save(fName);
}
else
{
bmp.Save(strSaveName);
}
}
return bmp;
}
}
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |