.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Thumbnail.OCX for .NET 64 bit

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
SRayC
1017 Views, 5 Replies

Thumbnail.OCX for .NET 64 bit

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.

5 REPLIES 5
Message 2 of 6
norman.yuan
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

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 6
SRayC
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)
Message 4 of 6
SRayC
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.
Message 5 of 6
norman.yuan
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

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 6
SRayC
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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost