Back in the day I used the Thumbnail.ocx to display a preview of a dwg file in a form.
I need to upgrade (re-write) an old VB6 program to the 21st centry using VB.NET and was wondering if there is a new version or a subsitute that I could use, maybe something from TrueView?
Any help is greatly appreciated.
Ray C.
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
You can find .NET code that retrieves thumbnail bitmap image from *.dwg file here:
http://www.theswamp.org/index.php?topic=30985.msg470910#msg470910
http://www.theswamp.org/index.php?topic=45598.msg507524#msg507524
Norman Yuan
FileStream.CopyTo() method is only available since .NET4.0. Since all my Acad development targets Acad2012 or later, I stoped using .NET2/3.x.
Yes, I used the code with minor change, worked OK with AutoCAD 2012's drawing. Here is the static class I use:
using System; using System.Drawing; using System.IO; namespace DrawingThumbnail { /// <summary> /// This code is originally from http://www.theswamp.org: /// http://www.theswamp.org/index.php?topic=30985.msg470910#msg470910 /// http://www.theswamp.org/index.php?topic=45598.msg507524#msg507524 /// Minor code change has be made /// </summary> public static class DwgThumbnail { public static Image GetBitmapFromDwgFile(string dwgFile) { if (!File.Exists(dwgFile)) { throw new FileNotFoundException("Cannot find file: \"" + dwgFile + "\""); } if (!Path.GetExtension(dwgFile).ToUpper().EndsWith("DWG")) { throw new ArgumentException("Wrong file type: not a *.DWG file."); } Image img = null; using (FileStream fs = new FileStream(dwgFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (BinaryReader br = new BinaryReader(fs)) { fs.Seek(0xd, SeekOrigin.Begin); fs.Seek(0x14 + br.ReadInt32(), SeekOrigin.Begin); byte bytCnt = br.ReadByte(); if (bytCnt <= 1) { return null; } int imageHeaderStart = 0; int imageHeaderSize = 0; byte imageCode = 0; for (short i = 1; i <= bytCnt; i++) { imageCode = br.ReadByte(); imageHeaderStart = br.ReadInt32(); imageHeaderSize = br.ReadInt32(); if (imageCode == 2) { //----------------------------------------- // BMP Preview (2012 file format) //----------------------------------------- // BITMAPINFOHEADER (40 bytes) br.ReadBytes(0xe); //biSize, biWidth, biHeight, biPlanes ushort biBitCount = br.ReadUInt16(); br.ReadBytes(4); //biCompression uint biSizeImage = br.ReadUInt32(); //br.ReadBytes(0x10); //biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant fs.Seek(imageHeaderStart, SeekOrigin.Begin); byte[] bitmapBuffer = br.ReadBytes(imageHeaderSize); uint colorTableSize = Convert.ToUInt32(Math.Truncate((biBitCount < 9) ? 4 * Math.Pow(2, biBitCount) : 0)); using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write(Convert.ToUInt16(0x4d42)); bw.Write(54u + colorTableSize + biSizeImage); bw.Write(new ushort()); bw.Write(new ushort()); bw.Write(54u + colorTableSize); bw.Write(bitmapBuffer); img = new Bitmap(ms); } } } else if (imageCode == 6) { //------------------------------------------- // PNG Preview (2013 file format) //------------------------------------------- fs.Seek(imageHeaderStart, SeekOrigin.Begin); using (MemoryStream ms = new MemoryStream()) { fs.CopyTo(ms, imageHeaderStart); img = Image.FromStream(ms); } } else if (imageCode == 3) { //return null; } } } } return img; } } }
HTH
Norman Yuan