Unable to create Thumbnail Image for AutoCAD

Unable to create Thumbnail Image for AutoCAD

Anonymous
Not applicable
1,537 Views
3 Replies
Message 1 of 4

Unable to create Thumbnail Image for AutoCAD

Anonymous
Not applicable

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;

        }

0 Likes
1,538 Views
3 Replies
Replies (3)
Message 2 of 4

Balaji_Ram
Alumni
Alumni

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

Message 3 of 4

Anonymous
Not applicable

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.

0 Likes
Message 4 of 4

Balaji_Ram
Alumni
Alumni

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