.NET

Reply
Valued Contributor
RamanSBV
Posts: 86
Registered: ‎02-28-2012
Message 1 of 4 (403 Views)

Unable to create Thumbnail Image for AutoCAD

403 Views, 3 Replies
04-23-2013 03:22 AM

Hi,

 

I am using below method for getting Bitmap object from autocad file.

 

This method is working fine for AutoCAD 2012 application

 

But same is not working in AutoCAD 2013 application

 

Please help me on this.

 

Regards,

Raman

       

publicBitmap GetAutoCADBitmap(string strFile, bool boolRetainBackColor)

        {

           

Bitmap bmp = newBitmap(1, 1, PixelFormat.Format8bppIndexed);

           

byte bytCnt = 0;

           

byte[] bytBMPBuff = null;

           

int lngImgLoc = 0;

           

BinaryReader br = null;

           

int lngCurLoc = 0;

           

int lngY = 0;

           

int lngX = 0;

           

int lngColor = 0;

           

int lngCnt = 0;

           

short intCnt = 0;

           

IMGREC udtRec = default(IMGREC);

           

RGBQUAD[] udtColors = null;

           

RGBQUAD udtColor = default(RGBQUAD);

           

BITMAPINFOHEADER udtHeader = default(BITMAPINFOHEADER);

           

short intRed = 0;

           

short intGreen = 0;

           

short intBlue = 0;

           

if (File.Exists(strFile))

            {

               

using (FileStream fs = newFileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

                {

                    br =

newBinaryReader(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 - 1; 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 =

newbyte[udtRec.lngLen + 1];

                               

if (udtHeader.biBitCount == 8)

                                {

                                    udtColors =

newRGBQUAD[256];

                                   

for (int count = 0; count <= 255; 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 =

newBitmap(udtHeader.biWidth, udtHeader.biHeight);

                                    lngCnt = 0;

                                   

for (lngY = 1; lngY <= udtHeader.biHeight; lngY++)

                                    {

                                       

for (lngX = udtHeader.biWidth; lngX >= 1; lngX += -1)

                                        {

                                            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 += 1;

                                        }

                                    }

                                }

                            }

                           

else

                            {

                               

if (udtRec.bytType == 3)

                                   

returnnull;

                            }

                        }

                    }

                   

else

                       

returnnull;

                }

            }

           

return bmp;

        }

ADN Support Specialist
Balaji_Ram
Posts: 666
Registered: ‎03-21-2011
Message 2 of 4 (339 Views)

Re: Unable to create Thumbnail Image for AutoCAD

05-02-2013 05:52 AM in reply to: RamanSBV

Hi Raman,

 

In 2013 Dwg file format, the thumbnail image is a PNG.

It is a BMP prior to 2013.

 

This discussion @ theswamp can help with your code :

 

http://www.theswamp.org/index.php?topic=30985.15



Balaji
Developer Technical Services
Autodesk Developer Network

New Member
8596547859
Posts: 1
Registered: ‎05-31-2013
Message 3 of 4 (280 Views)

Re: Unable to create Thumbnail Image for AutoCAD

05-31-2013 12:55 AM in reply to: Balaji_Ram

does this mean we need to c# convert bmp to png image, so that we can creat a thumnail for cad. it will be easy to me if so. i already download an amazing image app.

ADN Support Specialist
Balaji_Ram
Posts: 666
Registered: ‎03-21-2011
Message 4 of 4 (244 Views)

Re: Unable to create Thumbnail Image for AutoCAD

06-06-2013 11:08 AM in reply to: 8596547859

You do not need any conversion utility.

 

The thumbnail image is now stored in PNG format in the drawing and so while retieving it you will find a different image code.

 

Refer to the sample code in the link that I provided earlier. There is an additional check for imageCode equals 6.

 

Hope this explains.



Balaji
Developer Technical Services
Autodesk Developer Network

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