TheSwamp

Code Red => .NET => Topic started by: sinc on January 02, 2009, 12:36:12 PM

Title: Block Preview Icon
Post by: sinc on January 02, 2009, 12:36:12 PM
I am creating a block programatically, via the API, and then I want to display its PreviewIcon.  How do I do this?

After I create the block, there is no PreviewIcon.  I can run the BLOCKICON command, and then there's a PreviewIcon.  But how do I do this via the API, so I can display a preview of the block immediately after I create it?
Title: Re: Block Preview Icon
Post by: Spike Wilbury on January 02, 2009, 03:44:43 PM
I am creating a block programatically, via the API, and then I want to display its PreviewIcon.  How do I do this?

After I create the block, there is no PreviewIcon.  I can run the BLOCKICON command, and then there's a PreviewIcon.  But how do I do this via the API, so I can display a preview of the block immediately after I create it?

Do you mean to preview it in the Block command - little image box ?

I can get the previews there, of the blocks that are generated from my routines.

What's the code you are using or have so far?
Title: Re: Block Preview Icon
Post by: sinc on January 02, 2009, 04:39:17 PM
I can see it there, too.

It's this:

BlockTableRecord btr;  this is my block record
btr.PreviewIcon;  this is the thing that is empty until I run the BLOCKICON command


Sorry, it's a bit difficult to extract code, as it is spread through Forms controls and the working code extends over three different subprojects.

But I'm trying to solve the same problem as in this post, from almost four years ago:

http://discussion.autodesk.com/forums/message.jspa?messageID=4907295#4907295

Hopefully there's a solution to this issue by now.
Title: Re: Block Preview Icon
Post by: Spike Wilbury on January 02, 2009, 05:44:52 PM
I can see it there, too.

It's this:

BlockTableRecord btr;  this is my block record
btr.PreviewIcon;  this is the thing that is empty until I run the BLOCKICON command


Sorry, it's a bit difficult to extract code, as it is spread through Forms controls and the working code extends over three different subprojects.

But I'm trying to solve the same problem as in this post, from almost four years ago:

http://discussion.autodesk.com/forums/message.jspa?messageID=4907295#4907295

Hopefully there's a solution to this issue by now.

I see, I have AutoCAD 2007 and it is not implemented - the help said:

Quote
PreviewIcon (Read/write)
Type
System.Drawing.Bitmap


This method is not implemented in this release.

This method is not implemented in this release.

edit: added also this:
Quote


Up a level to AcDbBlockTableRecord         
AcDbBlockTableRecord::setPreviewIcon Function Acad::ErrorStatus

setPreviewIcon(

const PreviewIcon & previewIcon);

previewIcon Input PreviewIcon containing an array of bytes, represented in the form obtained from AcDbBlockTableRecord::getPreviewIcon().

Sets the block table record's preview icon with the array of bytes. This function is useful when copyng an icon from one block to another. To generate an icon, the BLOCKICON command or the Block dialog command should be used.
Title: Re: Block Preview Icon
Post by: sinc on January 02, 2009, 06:27:17 PM
That's the same thing that happens in C3D 2008.

However, you can get around it.  Instead of this:

Code: [Select]
bitmap = btr.PreviewIcon;
You have to include a reference to Autodesk.AutoCAD.Internal, and then use this:

Code: [Select]
bitmap = BlockThumbnailHelper.GetBlockThumbanail(btr.ObjectId);
(with the funky spelling - it really is spelled "GetBlockThumbanail").  But it's the same thing - there's nothing there until BLOCKICON has been run, and I still haven't been able to get it to work while my command is running.  Everything seems to go bad when I try to issue the BLOCKICON command using SendStringToExecute().
Title: Re: Block Preview Icon
Post by: sinc on January 02, 2009, 07:12:41 PM
I see what's happening in my code.

I create the block, then I issue the SendStringToExecute command.  However, this command is apparently being queued.  My code then pops open a dialog box that is supposed to display the block icon, but there is no block icon.  When the user hits OK in the dialog box, the user is prompted to pick some points on-screen.  It is at this point that the Editor.SelectPoint() method is processing the BLOCKICON command as keyword input.

So I'm still at the point of wondering how to get this icon refreshed without using the BLOCKICON command?  Or how do I get the BLOCKICON command to run while another command is already active?
Title: Re: Block Preview Icon
Post by: Kerry on January 02, 2009, 08:37:57 PM
Sinc',
I haven't played much with 2009 and not at all with C3D,

but,

I believe the 'Internal' is not neeed in 2009
and
Have you tried Tony's SendCommand method? I don't have a link, but you could perhaps find the link on the discussion Groups.

I recall having this discussion a few years ago and _BlockIcon Command seemed the solution, not sure of recent changes though.
Title: Re: Block Preview Icon
Post by: Kerry on January 02, 2009, 11:06:57 PM
weird,
In AC2008 Vanilla the HasPreviewIcon is false, but the BlockInsert Dialog displays the Icon.
I assume from that : the Dialog checks/or Builds it's the preview.


Code: [Select]
        [CommandMethod("BB1")]
        static public void BuildBlock_01()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            String blockName;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                blockName = DateTime.UtcNow.ToString().Replace("/", "").Replace(":", "");

                BlockTableRecord newBlockDef = new BlockTableRecord();
                newBlockDef.Name = blockName;
                BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

                blockTable.Add(newBlockDef);                                       
                tr.AddNewlyCreatedDBObject(newBlockDef, true);
                //
                Circle circle1 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 10);
                Circle circle2 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 12);
                newBlockDef.AppendEntity(circle1);
                newBlockDef.AppendEntity(circle2);
                tr.AddNewlyCreatedDBObject(circle1, true);
                tr.AddNewlyCreatedDBObject(circle2, true);

                tr.Commit();

                if (newBlockDef.HasPreviewIcon == true)
                {
                    ed.WriteMessage("Block {0} has a previewicon", blockName);
                }
            }
        }


(http://www.theswamp.org/screens/index.php?dir=KerryBrown/&file=BlockPreview_false.png)

(http://www.theswamp.org/screens/index.php?dir=KerryBrown/&file=BlockPreview.png)

... I'll keep playing

Title: Re: Block Preview Icon
Post by: Kerry on January 02, 2009, 11:10:17 PM
ps:
I don't usually name my blocks like that ... it just seemed the quickest way to get unique block names :)
Title: Re: Block Preview Icon
Post by: MP on January 02, 2009, 11:51:22 PM
(http://www.theswamp.org/screens/index.php?dir=KerryBrown/&file=BlockPreview.png)

Sorry to go off topic but ... purple text on an orange background? Yeow my eyes.
Title: Re: Block Preview Icon
Post by: Kerry on January 02, 2009, 11:57:52 PM
It's taken you 5 years to notice that ?

and to be pedantic, it's BLUE on Color 31
Title: Re: Block Preview Icon
Post by: Kerry on January 03, 2009, 12:05:31 AM
Wheee ..
Title: Re: Block Preview Icon
Post by: Kerry on January 03, 2009, 12:10:38 AM
using Tony's CommandLine.cs

from http://www.caddzone.com/CommandLine.cs (http://www.caddzone.com/CommandLine.cs)


ADDED:
If you can't see ALL the code in one pane, think about changing your display theme to Mercury in Look and Layout Preferences in your Member Profile
... If you aren't a Member yet .. register .. it's free.


Code: [Select]
/ CodeHimBelongaKwb   Jan2009

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;


using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

using CaddZone.ApplicationServices;
/// from http://www.caddzone.com/CommandLine.cs
///

[assembly: CommandClass(typeof(
    kdub_ACAD2008_VS2008_testing_103.TestCommands))]

namespace kdub_ACAD2008_VS2008_testing_103
{
    /// <summary>
    /// Summary description for TestCommands Class.
    /// </summary>
    public class TestCommands
    {
        public TestCommands()
        {   //
            // TODO: Add constructor logic here
            //
        }

        [CommandMethod("BB1")]
        static public void BuildBlock_01()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            String blockName;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                blockName = DateTime.UtcNow.ToString().Replace("/", "").Replace(":", "");

                BlockTableRecord newBlockDef = new BlockTableRecord();
                newBlockDef.Name = blockName;
                BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

                blockTable.Add(newBlockDef);                                       
                tr.AddNewlyCreatedDBObject(newBlockDef, true);
                //
                Circle circle1 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 10);
                Circle circle2 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 12);
                newBlockDef.AppendEntity(circle1);
                newBlockDef.AppendEntity(circle2);
                tr.AddNewlyCreatedDBObject(circle1, true);
                tr.AddNewlyCreatedDBObject(circle2, true);

                tr.Commit();

                CommandLine.Command("BlockIcon", blockName);

                if (newBlockDef.HasPreviewIcon == true)
                {
                    ed.WriteMessage("Block {0} has a previewicon", blockName);
                }
            }
        }
}

Title: Re: Block Preview Icon
Post by: Kerry on January 03, 2009, 12:30:18 AM
... and with a bit of additional code ...

[edit ADDED:]
If you can't see ALL the code in one pane, think about changing your display theme to Mercury in Look and Layout Preferences in your Member Profile
... If you aren't a Member yet .. register .. it's free.

.... and you'll be able to see the picture that is posted here ..
Title: Re: Block Preview Icon
Post by: sinc on January 03, 2009, 09:15:22 AM
Thanks, Kerry.

Unfortunately, that solution does me no good, because of Tony's strict rules for usage.
Title: Re: Block Preview Icon
Post by: sinc on January 03, 2009, 09:18:29 AM
Oh, wait, maybe I CAN use that.

I was looking closer at his usage statements, and this one looks different than some of the other ones I've seen on his site.  I've had to ignore things on his site before because of his strict allowances, but this one looks like it will let me use it.  At least, if I'm reading it correctly....
Title: Re: Block Preview Icon
Post by: sinc on January 03, 2009, 09:51:06 AM
I discovered that I can actually completely sidestep the issue, and avoid any possible problems by not using Tony's code.  Then I know for sure that I'm not going awry with any of his usage terms.

Instead, all I have to do is define this PInvoke call:

Code: [Select]
[SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl)]
extern static int acedCmd(IntPtr resbuf);

Then just do this:

Code: [Select]
using (ResultBuffer rb = new ResultBuffer())
{
     rb.Add(new TypedValue(5005, "._BLOCKICON"));
     rb.Add(new TypedValue(5005, blockName));
     acedCmd(rb.UnmanagedObject);
}

Problem solved!
Title: Re: Block Preview Icon
Post by: TonyT on January 04, 2009, 08:09:18 AM

I discovered that I can actually completely sidestep the issue, and avoid any possible problems by not using Tony's code.  Then I know for sure that I'm not going awry with any of his usage terms.


The only usage term is that the copyright notice appear in the credits
of any software you distribute that includes that code.

Have a problem with that?

Actually, you should probably consider crediting theswamp.org in
your application's credits, for helping you to the extent that it has,
which goes way beyond only what you've learned from my code.

Title: Re: Block Preview Icon
Post by: sinc on January 04, 2009, 09:39:06 AM
No, Tony, I have no problem with that, except for the fact that the legalese is difficult to interpret and parse.  Whenever someone posts lengthy legalese like that, it beholdens others to have to read and interpret it.

But as I said, once I took the time to carefully read this particular usage statement, I came back and said that it DID look like I could use it (although I am now certain I won't, because of this thread).  My comment was more because of a couple of other times I ended up on your site while researching a problem, and the other things I saw had a much more strict usage statement.  I did not read this one carefully, and just assumed it was the same as you other statements I had seen in the past.  I actually basically never go to your site, except for very very very very very rare occasions, because those first statements I saw did not give me the allowance to use the things you were posting.  I decided that, even though your site may be useful, it's not so useful if everything is restricted, so I stopped even looking at it.  This was the first time I've even looked at your site in I-don't-know-how-many-months.  And now, I do not intend to ever look at your site again.

And if you really think I should credit the Swamp in my application, then what should I do with all the other sources of teaching I've had over the years?  In a lengthy career in software development, I've learned far, far, far, far, far more from other sources than I have from the Swamp.  So where does it end?  How many people do I have to credit in my application?   How many people do YOU credit in YOUR applications?  After all, you learned what you know from SOMEWHERE.  You weren't born with the knowledge.
Title: Re: Block Preview Icon
Post by: TonyT on January 04, 2009, 10:16:16 AM
I don't see how the legalese is difficult to parse. 

I made it completely free for any purpose whatsoever, with the
only condition that I'm credited for using it. If you would rather
not give credit, that's fine, but there's no need to distort that.

No, Tony, I have no problem with that, except for the fact that the legalese is difficult to interpret and parse.  Whenever someone posts lengthy legalese like that, it beholdens others to have to read and interpret it.

But as I said, once I took the time to carefully read this particular usage statement, I came back and said that it DID look like I could use it (although I am now certain I won't, because of this thread).  My comment was more because of a couple of other times I ended up on your site while researching a problem, and the other things I saw had a much more strict usage statement.  I did not read this one carefully, and just assumed it was the same as you other statements I had seen in the past.  I actually basically never go to your site, except for very very very very very rare occasions, because those first statements I saw did not give me the allowance to use the things you were posting.  I decided that, even though your site may be useful, it's not so useful if everything is restricted, so I stopped even looking at it.  This was the first time I've even looked at your site in I-don't-know-how-many-months.  And now, I do not intend to ever look at your site again.

And if you really think I should credit the Swamp in my application, then what should I do with all the other sources of teaching I've had over the years?  In a lengthy career in software development, I've learned far, far, far, far, far more from other sources than I have from the Swamp.  So where does it end?  How many people do I have to credit in my application?   How many people do YOU credit in YOUR applications?  After all, you learned what you know from SOMEWHERE.  You weren't born with the knowledge.
Title: Re: Block Preview Icon
Post by: pb.Perksa on March 22, 2009, 07:56:06 PM
Sigh... Tony Tony Tony... (as in Tanzillo).  You really are your own worst enemy sometimes mate.  You really know your stuff, but getting past the invective is hard, and over the years, I've learned its just more effort than its worth reading your stuff.  It's easier just to ignore it and get it from another source. 

Anyways...

The best method I've ever found for extracting the image preview from a drawing (and certainly the fastest) is something I found on codeproject long ago...

http://www.codeproject.com/KB/vbscript/VBDwgImageExtractor.aspx

I used this code to create a block directory list program, that read the bitmaps out of each file on-the-fly.  Now I just point my external app at the directory from a tool-palette, and drop the blocks where I need to maintain an appropriate discipline-based grouping.
Title: Re: Block Preview Icon
Post by: pb.Perksa on March 22, 2009, 08:00:27 PM
This is it in my VB.NET application.  I'd appreciate if someone actually took the time to do this in C# some time... It should be fairly straight-forward to modify one would assume...

Just pass the filename, and the picturebox to the function, and voila.

Best part about it is that it doesn't require AutoCAD at all.

Quote
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.ApplicationServices
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging

Imports System.Windows.forms

Module modACADIconPreview

   Public Function ACADIconPreview(ByVal strFile As String, ByVal picBox As PictureBox) As Integer
      Dim Num1 As Integer = 1
      Dim imgVal1 As Image = Nothing

      ' Create the reader for data.
      Dim fs As FileStream = New FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
      Dim r As BinaryReader = New BinaryReader(fs)

      ' Get the image position in the DWG file
      r.BaseStream.Seek(&HD, SeekOrigin.Begin)
      Dim imgPos As Int32 = r.ReadInt32()
      r.BaseStream.Seek(imgPos, SeekOrigin.Begin)

      ' Image sentinel to check if the image data

      ' is not corrupted
      Dim imgBSentinel() As Byte = {&H1F, &H25, &H6D, &H7, &HD4, &H36, &H28, &H28, &H9D, &H57, &HCA, &H3F, &H9D, &H44, &H10, &H2B}

      ' Read Image sentinel
      Dim imgCSentinel(16) As Byte
      imgCSentinel = r.ReadBytes(16)

      ' if image sentinel is correct
      If (imgBSentinel.ToString() = imgCSentinel.ToString()) Then
         ' Get image size
         Dim imgSize As UInt32 = r.ReadUInt32()

         ' Get number of images present
         Dim imgPresent As Byte = r.ReadByte()

         ' header
         Dim imgHeaderStart As Int32 = 0
         Dim imgHeaderSize As Int32 = 0

         ' bmp data
         Dim imgBmpStart As Int32 = 0
         Dim imgBmpSize As Int32 = 0
         Dim bmpDataPresent As Boolean = False

         ' wmf data
         Dim imgWmfStart As Int32
         Dim imgWmfSize As Int32
         Dim wmfDataPresent As Boolean = False

         ' get each image present
         For I As Integer = 1 To imgPresent
            ' Get image type

            Dim imgCode As Byte = r.ReadByte()
            Select Case imgCode

               Case 1
                  ' Header data
                  imgHeaderStart = r.ReadInt32()
                  imgHeaderSize = r.ReadInt32()
               Case 2
                  ' bmp data
                  imgBmpStart = r.ReadInt32()
                  imgBmpSize = r.ReadInt32()
                  bmpDataPresent = True
               Case 3
                  ' wmf data
                  imgWmfStart = r.ReadInt32()
                  imgWmfSize = r.ReadInt32()
                  wmfDataPresent = True
            End Select

         Next

         If (bmpDataPresent) Then

            r.BaseStream.Seek(imgBmpStart, SeekOrigin.Begin)

            Dim tempPixelData(imgBmpSize + 14) As Byte

            ' indicate it is a bit map
            tempPixelData(0) = &H42
            tempPixelData(1) = &H4D

            ' offBits
            tempPixelData(10) = &H36
            tempPixelData(11) = &H4

            Dim tempBuffData(imgBmpSize) As Byte

            tempBuffData = r.ReadBytes(imgBmpSize)
            tempBuffData.CopyTo(tempPixelData, 14)

            Dim memStream As MemoryStream = New MemoryStream(tempPixelData)
            Dim bmp As Bitmap = New Bitmap(memStream)
            Dim szBmp As System.Drawing.Size
            szBmp.Width = picBox.Width
            szBmp.Height = picBox.Height
            Dim bmpResize As Bitmap = New Bitmap(bmp, szBmp)   'resize the bitmap for the picturebox

            picBox.Image = bmpResize

         End If

         If (wmfDataPresent) Then
            ' read imgWmfSize wmf data

         End If

         Dim imgESentinel() As Byte = {&HE0, &HDA, &H92, &HF8, &H2B, &HC9, &HD7, &HD7, &H62, &HA8, &H35, &HC0, &H62, &HBB, &HEF, &HD4}
         imgCSentinel = r.ReadBytes(16)

         ' if image sentinel is correct
         If (imgESentinel.ToString() = imgCSentinel.ToString()) Then
            ' Image data is not corrupted

         End If

      End If

      fs.Close()
     
   End Function

End Module
Title: Re: Block Preview Icon
Post by: pb.Perksa on June 09, 2009, 08:17:18 PM
bump
Title: Re: Block Preview Icon
Post by: kdub on June 09, 2009, 09:42:13 PM


UHMmmmm  ... what's the bump for ?
Title: Re: Block Preview Icon
Post by: It's Alive! on June 09, 2009, 10:31:19 PM
.
Title: Re: Block Preview Icon
Post by: bikelink on November 17, 2009, 01:12:05 AM
my job for c#

 public static void  ACADIconPreview(String strFile  , PictureBox  picBox  )
         {
            int Num1 = 1;
            Image imgVal1 = null;

             
             System.IO.FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader r   = new  BinaryReader(fs);


              // Get the image position in the DWG file
             long l = byte.Parse("D", NumberStyles.AllowHexSpecifier);
              r.BaseStream.Seek(l, SeekOrigin.Begin);
              int  imgPos  = r.ReadInt32 ();
              r.BaseStream.Seek(imgPos, SeekOrigin.Begin);

      // Image sentinel to check if the image data
   
      // is not corrupted
      Encoding unicode = Encoding.Unicode;


      Byte[] imgBSentinel = { byte.Parse("1F", NumberStyles.AllowHexSpecifier),
                            byte.Parse("25",  NumberStyles.AllowHexSpecifier),
                            byte.Parse("6D", NumberStyles.AllowHexSpecifier), 
                            byte.Parse("7", NumberStyles.AllowHexSpecifier),  byte.Parse("D4", NumberStyles.AllowHexSpecifier), 
             byte.Parse("36", NumberStyles.AllowHexSpecifier),  byte.Parse("28", NumberStyles.AllowHexSpecifier), 
             byte.Parse("28", NumberStyles.AllowHexSpecifier),  byte.Parse("9D", NumberStyles.AllowHexSpecifier),  byte.Parse("57", NumberStyles.AllowHexSpecifier),
            byte.Parse("CA", NumberStyles.AllowHexSpecifier), 
             byte.Parse("3F", NumberStyles.AllowHexSpecifier),  byte.Parse("9D", NumberStyles.AllowHexSpecifier),
            byte.Parse("44", NumberStyles.AllowHexSpecifier),  byte.Parse("10", NumberStyles.AllowHexSpecifier), 
             byte.Parse("2B", NumberStyles.AllowHexSpecifier)
         };

      // Read Image sentinel
      Byte[] imgCSentinel = new Byte[16];
      imgCSentinel = r.ReadBytes(16);

      // if image sentinel is correct
      if  (imgBSentinel.ToString() == imgCSentinel.ToString())
      {
         // Get image size
         int imgSize  = r.ReadInt32 ();

         // Get number of images present
         Byte imgPresent   = r.ReadByte();

         // header
         int imgHeaderStart   = 0;
         int  imgHeaderSize   = 0;

         // bmp data
         int imgBmpStart   = 0;
         int imgBmpSize   = 0;
         bool  bmpDataPresent  = false;

         // wmf data
         int imgWmfStart , imgWmfSize ;
         bool wmfDataPresent  = false;

         // get each image present
         for(int Iint = 0 ; Iint < imgPresent; Iint++)
         {

               Byte  imgCode  = r.ReadByte();
               switch(imgCode)
               {
                   case 1:
                      // Header data
                      imgHeaderStart = r.ReadInt32 ();
                      imgHeaderSize = r.ReadInt32();
                      break;
                   case 2:
                      // bmp data
                      imgBmpStart = r.ReadInt32();
                      imgBmpSize = r.ReadInt32();
                      bmpDataPresent = true;
                      break;
                   case 3:
                      // wmf data
                      imgWmfStart = r.ReadInt32();
                      imgWmfSize = r.ReadInt32();
                      wmfDataPresent = true;
                      break;
                   default:
                       break;
             }

        }

         if (bmpDataPresent)
         {

            r.BaseStream.Seek(imgBmpStart, SeekOrigin.Begin);

            Byte[]  tempPixelData =new byte[imgBmpSize + 14];

            // indicate it is a bit map
            tempPixelData[0] = byte.Parse("H42", CultureInfo.InvariantCulture);
            tempPixelData[1] = byte.Parse("H4D", CultureInfo.InvariantCulture);

            // offBits
            tempPixelData[10] = byte.Parse("H36", CultureInfo.InvariantCulture);
            tempPixelData[11] =  byte.Parse("H4", CultureInfo.InvariantCulture);

            Byte[]  tempBuffData = new byte[imgBmpSize] ;

            tempBuffData = r.ReadBytes(imgBmpSize);
            tempBuffData.CopyTo(tempPixelData, 14);

            MemoryStream memStream  = new  MemoryStream(tempPixelData);
            Bitmap  bmp  = new Bitmap(memStream);
            Size szBmp = new System.Drawing.Size();
            szBmp.Width = picBox.Width;
            szBmp.Height = picBox.Height;
           Bitmap  bmpResize  = new Bitmap(bmp, szBmp)  ;
            picBox.Image = bmpResize;

         }

         
          // read imgWmfSize wmf data
         if (wmfDataPresent)
         {}
           
       
         Byte[] imgESentinel = {byte.Parse("E0", NumberStyles.AllowHexSpecifier),
             byte.Parse("DA", NumberStyles.AllowHexSpecifier), byte.Parse("92", NumberStyles.AllowHexSpecifier),
             byte.Parse("F8", NumberStyles.AllowHexSpecifier), byte.Parse("2B", NumberStyles.AllowHexSpecifier),
             byte.Parse("C9", NumberStyles.AllowHexSpecifier), byte.Parse("D7", NumberStyles.AllowHexSpecifier),
             byte.Parse("D7", NumberStyles.AllowHexSpecifier), byte.Parse("62", NumberStyles.AllowHexSpecifier),
             byte.Parse("A8", NumberStyles.AllowHexSpecifier), byte.Parse("35", NumberStyles.AllowHexSpecifier),
             byte.Parse("C0", NumberStyles.AllowHexSpecifier), byte.Parse("62", NumberStyles.AllowHexSpecifier),
             byte.Parse("BB", NumberStyles.AllowHexSpecifier), byte.Parse("EF", NumberStyles.AllowHexSpecifier),
             byte.Parse("D4", NumberStyles.AllowHexSpecifier)};         


         imgCSentinel = r.ReadBytes(16);

         // if image sentinel is correct
         if (imgESentinel.ToString() == imgCSentinel.ToString())
         {}
         

      }

      fs.Close();
     
         }
Title: Re: Block Preview Icon
Post by: rodrigo_gcmsoft on April 02, 2011, 11:06:40 PM
my job for c#

 public static void  ACADIconPreview(String strFile  , PictureBox  picBox  )
         {
            int Num1 = 1;
            Image imgVal1 = null;

             
             System.IO.FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader r   = new  BinaryReader(fs);


              // Get the image position in the DWG file
             long l = byte.Parse("D", NumberStyles.AllowHexSpecifier);
              r.BaseStream.Seek(l, SeekOrigin.Begin);
              int  imgPos  = r.ReadInt32 ();
              r.BaseStream.Seek(imgPos, SeekOrigin.Begin);

      // Image sentinel to check if the image data
   
      // is not corrupted
      Encoding unicode = Encoding.Unicode;


      Byte[] imgBSentinel = { byte.Parse("1F", NumberStyles.AllowHexSpecifier),
                            byte.Parse("25",  NumberStyles.AllowHexSpecifier),
                            byte.Parse("6D", NumberStyles.AllowHexSpecifier), 
                            byte.Parse("7", NumberStyles.AllowHexSpecifier),  byte.Parse("D4", NumberStyles.AllowHexSpecifier), 
             byte.Parse("36", NumberStyles.AllowHexSpecifier),  byte.Parse("28", NumberStyles.AllowHexSpecifier), 
             byte.Parse("28", NumberStyles.AllowHexSpecifier),  byte.Parse("9D", NumberStyles.AllowHexSpecifier),  byte.Parse("57", NumberStyles.AllowHexSpecifier),
            byte.Parse("CA", NumberStyles.AllowHexSpecifier), 
             byte.Parse("3F", NumberStyles.AllowHexSpecifier),  byte.Parse("9D", NumberStyles.AllowHexSpecifier),
            byte.Parse("44", NumberStyles.AllowHexSpecifier),  byte.Parse("10", NumberStyles.AllowHexSpecifier), 
             byte.Parse("2B", NumberStyles.AllowHexSpecifier)
         };

      // Read Image sentinel
      Byte[] imgCSentinel = new Byte[16];
      imgCSentinel = r.ReadBytes(16);

      // if image sentinel is correct
      if  (imgBSentinel.ToString() == imgCSentinel.ToString())
      {
         // Get image size
         int imgSize  = r.ReadInt32 ();

         // Get number of images present
         Byte imgPresent   = r.ReadByte();

         // header
         int imgHeaderStart   = 0;
         int  imgHeaderSize   = 0;

         // bmp data
         int imgBmpStart   = 0;
         int imgBmpSize   = 0;
         bool  bmpDataPresent  = false;

         // wmf data
         int imgWmfStart , imgWmfSize ;
         bool wmfDataPresent  = false;

         // get each image present
         for(int Iint = 0 ; Iint < imgPresent; Iint++)
         {

               Byte  imgCode  = r.ReadByte();
               switch(imgCode)
               {
                   case 1:
                      // Header data
                      imgHeaderStart = r.ReadInt32 ();
                      imgHeaderSize = r.ReadInt32();
                      break;
                   case 2:
                      // bmp data
                      imgBmpStart = r.ReadInt32();
                      imgBmpSize = r.ReadInt32();
                      bmpDataPresent = true;
                      break;
                   case 3:
                      // wmf data
                      imgWmfStart = r.ReadInt32();
                      imgWmfSize = r.ReadInt32();
                      wmfDataPresent = true;
                      break;
                   default:
                       break;
             }

        }

         if (bmpDataPresent)
         {

            r.BaseStream.Seek(imgBmpStart, SeekOrigin.Begin);

            Byte[]  tempPixelData =new byte[imgBmpSize + 14];

            // indicate it is a bit map
            tempPixelData[0] = byte.Parse("H42", CultureInfo.InvariantCulture);
            tempPixelData[1] = byte.Parse("H4D", CultureInfo.InvariantCulture);

            // offBits
            tempPixelData[10] = byte.Parse("H36", CultureInfo.InvariantCulture);
            tempPixelData[11] =  byte.Parse("H4", CultureInfo.InvariantCulture);

            Byte[]  tempBuffData = new byte[imgBmpSize] ;

            tempBuffData = r.ReadBytes(imgBmpSize);
            tempBuffData.CopyTo(tempPixelData, 14);

            MemoryStream memStream  = new  MemoryStream(tempPixelData);
            Bitmap  bmp  = new Bitmap(memStream);
            Size szBmp = new System.Drawing.Size();
            szBmp.Width = picBox.Width;
            szBmp.Height = picBox.Height;
           Bitmap  bmpResize  = new Bitmap(bmp, szBmp)  ;
            picBox.Image = bmpResize;

         }

         
          // read imgWmfSize wmf data
         if (wmfDataPresent)
         {}
           
       
         Byte[] imgESentinel = {byte.Parse("E0", NumberStyles.AllowHexSpecifier),
             byte.Parse("DA", NumberStyles.AllowHexSpecifier), byte.Parse("92", NumberStyles.AllowHexSpecifier),
             byte.Parse("F8", NumberStyles.AllowHexSpecifier), byte.Parse("2B", NumberStyles.AllowHexSpecifier),
             byte.Parse("C9", NumberStyles.AllowHexSpecifier), byte.Parse("D7", NumberStyles.AllowHexSpecifier),
             byte.Parse("D7", NumberStyles.AllowHexSpecifier), byte.Parse("62", NumberStyles.AllowHexSpecifier),
             byte.Parse("A8", NumberStyles.AllowHexSpecifier), byte.Parse("35", NumberStyles.AllowHexSpecifier),
             byte.Parse("C0", NumberStyles.AllowHexSpecifier), byte.Parse("62", NumberStyles.AllowHexSpecifier),
             byte.Parse("BB", NumberStyles.AllowHexSpecifier), byte.Parse("EF", NumberStyles.AllowHexSpecifier),
             byte.Parse("D4", NumberStyles.AllowHexSpecifier)};         


         imgCSentinel = r.ReadBytes(16);

         // if image sentinel is correct
         if (imgESentinel.ToString() == imgCSentinel.ToString())
         {}
         

      }

      fs.Close();
     
         }

Hello, could you help me with your code?
I am facing an error in the following lines:
tempPixelData[0] = byte.Parse ("H42");
tempPixelData[1] = byte.Parse ("H4D");
"Input string was not in a correct format"
Could you help me please?
Thanks