.NET

Reply
Distinguished Contributor
SRayC
Posts: 227
Registered: ‎12-10-2003
Message 1 of 6 (236 Views)
Accepted Solution

Thumbnail.OCX for .NET 64 bit

236 Views, 5 Replies
02-14-2014 06:31 AM

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.

*Expert Elite*
norman.yuan
Posts: 946
Registered: ‎04-27-2009
Message 2 of 6 (219 Views)

Re: Thumbnail.OCX for .NET 64 bit

02-14-2014 08:45 AM in reply to: SRayC

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

 

 

 

 

Distinguished Contributor
SRayC
Posts: 227
Registered: ‎12-10-2003
Message 3 of 6 (207 Views)

Re: Thumbnail.OCX for .NET 64 bit

02-14-2014 11:09 AM in reply to: norman.yuan
Looks great.
This is a bit over my head but when I create my class it is throwing an error on the line
fs.CopyTo(ms, imageHeaderStart)
Distinguished Contributor
SRayC
Posts: 227
Registered: ‎12-10-2003
Message 4 of 6 (208 Views)

Re: Thumbnail.OCX for .NET 64 bit

02-14-2014 11:12 AM in reply to: SRayC
Sorry about that hit the worng key and submitted the post to quick.

As I was syaing it is posting an development error that
'CopyTo' is not a member of 'System.IO.FileStream'
Have you had a chance to use that code?


Thanks for the help

Ray C.
*Expert Elite*
norman.yuan
Posts: 946
Registered: ‎04-27-2009
Message 5 of 6 (198 Views)

Re: Thumbnail.OCX for .NET 64 bit

02-14-2014 11:58 AM in reply to: SRayC

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

Distinguished Contributor
SRayC
Posts: 227
Registered: ‎12-10-2003
Message 6 of 6 (196 Views)

Re: Thumbnail.OCX for .NET 64 bit

02-14-2014 12:01 PM in reply to: norman.yuan
That's what I concluded. I'm using VS2008, does not support .NET 4.0.
Time for an upgrade.
Thanks for all your help.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community