Author Topic: Ex: Show Thumbnail in PictureBox VB.NET  (Read 21358 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Ex: Show Thumbnail in PictureBox VB.NET
« on: September 15, 2010, 03:17:12 PM »
I ran across this post

http://forums.augi.com/showthread.php?t=121671

Here is a quote from the famous Kerry Brown

Quote
I know this may sound like a really wild suggestion, but
why don't you ask the original poster at theSwamp ?
and perhaps thank him for posting the code at the same time.

Which was replied with

Quote
Boo...

Not to step on toes here but I'm kinda a one forum guy and I asked my original question here. The answers (at this forum) might actually help others (at this forum) instead of some sort of endless link after link into another forum... and then into another. I REALLY DO appreciate the link to the swamp's forum solution and the original poster at that forum, but the personal experience and knowledge of mjohnston.26901 might take me a bit further into my question and solution with his/her personal take on the code and not to mention in the wake of all this... stuff... might help others... here, at this forum, the forum I signed up for and goto the most.

So naturally I had a little free time and simple thing to do so I wrote a VB.NET App and will post it here

If you appoligize to Kerry I will add the code to double click the Picture box and bring all entites from the drawing into the current dwg
or layouts or plotSettings or whatever


Also it was written VS2010 I will convert it VS2008 if you need it.




Glenn R

  • Guest
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #1 on: September 15, 2010, 03:45:27 PM »
Don't bother converting it. I agree with Kerry wholeheartedly. His suggestion was valid and went straight over their ungrateful heads. Let 'em rot.

Another Useless Group of Individuals.

LE3

  • Guest
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #2 on: September 15, 2010, 04:04:36 PM »
Another Useless Group of Individuals.

 :lol:

jgr

  • Guest
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #3 on: September 15, 2010, 04:19:49 PM »
I do not speak English and maybe I'm wrong or not understand the issue.

But if you need to read the preview image without using AutoCAD you can use this:
(Also works with 32-bit images (Yes, thumbnail may be 32-bit), 100% dotNet)


Code: [Select]
Imports System
Imports System.Drawing
Imports System.IO

Friend NotInheritable Class ThumbnailReader

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _
    Private Structure tagBITMAPFILEHEADER

        Public bfType As UShort

        Public bfSize As UInteger

        Public bfReserved1 As UShort

        Public bfReserved2 As UShort

        Public bfOffBits As UInteger
    End Structure

    Friend Shared Function GetThumbnail(ByVal filename As String) As Bitmap

        Using fs As FileStream = File.OpenRead(filename)
            Using br As BinaryReader = New BinaryReader(fs)
                Dim datLoc As Integer
                Dim bytCnt As Byte

                ' TODO
                ' Modificar para leer directamente el centinela
                fs.Seek(13, SeekOrigin.Begin)
                datLoc = br.ReadInt32()
                datLoc += 20
                fs.Seek(datLoc, SeekOrigin.Begin)
                bytCnt = br.ReadByte()

                If bytCnt <= 1 Then
                    Return Nothing
                End If

                Dim imgHeaderStart As Int32
                Dim imgHeaderSize As Int32
                Dim imgCode As Byte

                For i As Short = 1 To bytCnt

                    imgCode = br.ReadByte()
                    imgHeaderStart = br.ReadInt32()
                    imgHeaderSize = br.ReadInt32()

                    If imgCode = 2 Then
                        fs.Seek(imgHeaderStart, SeekOrigin.Begin)

                        '-----------------------------------------------------
                        ' TODO
                        ' Deberia comprobar si asi es mas rapido, o leer cada bloque uno a uno
                        Dim bmpHeader(40) As Byte
                        bmpHeader = br.ReadBytes(40)

                        Dim biBitCount As UShort = BitConverter.ToUInt16(New Byte() {bmpHeader(14), bmpHeader(15)}, 0)
                        Dim biSizeImage As UInteger = BitConverter.ToUInt16(New Byte() {bmpHeader(20), bmpHeader(21), bmpHeader(22), bmpHeader(23)}, 0)
                        '-----------------------------------------------------

                        fs.Seek(imgHeaderStart, SeekOrigin.Begin)

                        Dim bytBMPBuff(imgHeaderSize) As Byte
                        bytBMPBuff = br.ReadBytes(imgHeaderSize)

                        Dim clrTableSize As UInteger = CUInt(If(biBitCount < 9, 4 * (2 ^ biBitCount), 0))
                        Dim bfHeader As tagBITMAPFILEHEADER

                        With bfHeader
                            .bfType = &H4D42
                            .bfSize = CUInt(54) + clrTableSize + biSizeImage
                            .bfOffBits = CUInt(54) + clrTableSize
                        End With

                        Using ms As New MemoryStream

                            Using bw As BinaryWriter = New BinaryWriter(ms)
                                bw.Write(bfHeader.bfType)
                                bw.Write(bfHeader.bfSize)
                                bw.Write(bfHeader.bfReserved1)
                                bw.Write(bfHeader.bfReserved2)
                                bw.Write(bfHeader.bfOffBits)
                                bw.Write(bytBMPBuff)

                                Dim bm As Bitmap = New Bitmap(ms)
                                Return bm
                            End Using

                        End Using



                    ElseIf imgCode = 3 Then

                        Return Nothing
                    End If

                Next

            End Using
        End Using

        Return Nothing
    End Function

End Class

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #4 on: September 15, 2010, 04:28:20 PM »
I love it  :evil:
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #5 on: September 15, 2010, 04:36:07 PM »
I was just trying to help the guy out. AutoCAD .NET is not like C# or C++ or whatever where you can you walk in bookstore and find ten books or a huge amount resources online. You will learn something everywhere which brings me to this.

This is used to fill a ImageList for a ListView Control
Code: [Select]
Database db = new Database(false, false)
db.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, false, null);
Bitmap bitmap = db.ThumbnailBitmap;
if (!(DetailsLargeImage.Images.ContainsKey(filename)))
 {
    DetailsLargeImage.Images.Add(filename, bitmap);
    DetailsSmallImage.Images.Add(filename, bitmap);
   
   }

This was to set the picture box Image and if I used FileOpenMode.OpenForReadAndAllShare like above I got an error 
Code: [Select]
        Dim fName As String = cboFileNmae.Text
        Dim db As Database = New Database(False, False)
        db.ReadDwgFile(fName, FileOpenMode.OpenForReadAndReadShare, False, "")
        Dim tmbNail As Bitmap = db.ThumbnailBitmap
        picBoxThumbnail.Image = tmbNail

I really do not see what the difference would be for setting a PictureBox image and filling a ImageList would be

Somehow a line got left out
« Last Edit: September 15, 2010, 05:36:05 PM by fro2001 »

Glenn R

  • Guest
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #6 on: September 15, 2010, 05:00:07 PM »
fro,

I was not having a go at you by any means. It was great you brought up the post on AUGI.

Glenn R

  • Guest
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #7 on: September 15, 2010, 05:04:49 PM »
jgr,

That's excellent!

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #8 on: September 15, 2010, 05:15:35 PM »
fro,

I was not having a go at you by any means. It was great you brought up the post on AUGI.

Sorry I did not mean to imply that all
For future reference if I get out line or (I was going to say do something stupid but that is going happen alot) I hope you let me know

jgr

  • Guest
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #9 on: September 15, 2010, 05:20:05 PM »
I really do not see what the difference would be for setting a PictureBox image and filling a ImageList would be

Are you sure?
Imagelist.imageSize is not as bitmap.width, bitmap.heigth

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #10 on: September 15, 2010, 05:22:50 PM »
For future reference if I get out line or (I was going to say do something stupid but that is going happen alot) I hope you let me know
Join the club, I depend on Glenn and Kerry to keep me in line
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #11 on: September 15, 2010, 05:50:55 PM »
I really do not see what the difference would be for setting a PictureBox image and filling a ImageList would be

Are you sure?
Imagelist.imageSize is not as bitmap.width, bitmap.heigth

First of all jgr excellent post I added a second picture box to the above app and added the line
picBoxJGR.Image = ThumbnailReader.GetThumbnail(fName)
And worked great I am curious which is faster will test later?

Here I go doing something stupid again making it more complicated than it is I was getting the error at db.ReadDwgFile
Which was before I even assign the bitmap or anything to do with ImageList or PictureBox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #12 on: September 15, 2010, 08:21:42 PM »

I'm reminded of my grandfather ..
"You can lead a horse to water but can't give it a map."
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

jgr

  • Guest
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #13 on: September 15, 2010, 08:35:32 PM »
 :oops: Now I understand. (I think)

My code is also based on acThumnailReader  (vb6, forever!) + Open Design Specification for .dwg files
(acThumnailReader is based on activedwgpreview (vb6) ... this is based on...this....)

 :oops: :oops: :oops: :angel:

Sorry, my code isbased on acBMPPreview (100% vb6).  Not on "acThumnailReader"
« Last Edit: September 15, 2010, 08:59:12 PM by jgr »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #14 on: September 15, 2010, 09:01:14 PM »

I'm reminded of my grandfather ..
"You can lead a horse to water but can't give it a map."


Code: [Select]
Yours courteously,
etc...

 :-D

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #15 on: September 19, 2010, 04:53:59 PM »

jgr

  • Guest
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #16 on: September 19, 2010, 05:36:47 PM »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #17 on: September 20, 2010, 02:33:00 AM »
I think the blockIcon command  generates  32x32x8bpp bitmaps,  I was able to create them via AcGsDevice/ AcGsView && MFC. 
http://www.theswamp.org/index.php?topic=33116.msg401299#msg401299

For .net have a look at Autodesk.AutoCAD.GraphicsSystem.GetSnapshot(Rectangle rectangle); You should be able to render the block and take a snapshot,  I think you can do this via OffScreenDevice so you're not capturing off a control like I did

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #18 on: September 20, 2010, 03:02:24 AM »
I use PreviewIcon with no problem
but this was a reply for CapturePreviewImage which broke in 2011 and the drawing does not have to be active
Quote
The behavior has been already reported. The current status for the Change Request is fixed and it should be available in the upcoming AutoCAD update.
As a workaround, The property “Layout.Thumbnail” should give you the layout/paper space thumbnail bitmap.”

Or if you need a thumbnail of the model space you can use the code I provided in my initial reply.


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #19 on: September 20, 2010, 03:05:43 AM »
DoH! sorry, my mistake, just ignore me   :whistle:

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #20 on: September 20, 2010, 03:07:05 AM »
This one brings up a dialog box though

Quote

 <DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode)> _
Private Shared Function acedCommand(ByVal type1 As Integer, ByVal command As String, ByVal type2 As Integer, ByVal blockName As String, ByVal [end] As Integer) As Integer

        If btr.PreviewIcon Is Nothing Then
            acedCommand(5005, "BLOCKICON", 5005, blkName, 5000)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #21 on: September 20, 2010, 03:08:36 AM »
Did you figure out in .NET another way for blocks with out having to call BlockIcon 

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #22 on: September 20, 2010, 03:28:57 AM »
Not with .NET, but I assume you can use the classes within Autodesk.AutoCAD.GraphicsSystem to render, view, or get a snapshot of any entity(s) just as you can with the underlying AcGs classes.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #23 on: September 20, 2010, 04:02:09 AM »
That NameSpace does not like me every thing I played around with in it has crashed or just leaves a bunch of stuff all over the screen including code from ADN but I am pretty sure it has to do with I have no idea what I doing?

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #24 on: September 20, 2010, 05:30:15 AM »
The Database.ThumbnailBitmap reads like 200 out of 200 iterating through a folder

This one read 0 out of a 200

Code: [Select]

        [System.Security.SuppressUnmanagedCodeSecurity(), DllImport("acdb18.dll", CallingConvention = CallingConvention.Cdecl,
         EntryPoint = "?acdbGetPreviewBitmap@@YAPEAUtagBITMAPINFO@@PEB_W@Z")]
        private static extern IntPtr acdbGetPreviewBitmap([MarshalAs(UnmanagedType.LPWStr)] string sFilename);

        System.Drawing.Bitmap GetBitmapFromDwg(string filename)
        {
            IntPtr bmpInfo = acdbGetPreviewBitmap(filename);
            return Autodesk.AutoCAD.Runtime.Marshaler.BitmapInfoToBitmap(bmpInfo);
        }

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #25 on: September 20, 2010, 06:14:45 AM »
I'm a little confused are you trying to get the PreviewBitmap for the drawing or the PreviewIcon(still bitmaps) from the BlockTableRecord?

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #26 on: September 20, 2010, 12:49:04 PM »
Sorry
There all thrown together
Basically I have no problems getting either a BlockTableRecord's or "drawings or layout's" bitmap's closed or open, but I have not found a stable method for generating one if it does not exist.


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #27 on: September 21, 2010, 03:57:39 AM »
Simple, use the  AcGsVew classes as I mentioned earlier

here is a snippit of a form, just use your block or any other entity

Code: [Select]
namespace ExecMethod
{
  public partial class ViewFrm : Form
  {
    AcGs.Manager manager = null;
    AcGs.Device device = null;
    AcGs.Model model = null;
    AcGs.View view = null;

    public ViewFrm()
    {

      InitializeComponent();

      manager = AcAp.Application.DocumentManager.MdiActiveDocument.GraphicsManager;
      device = manager.CreateAutoCADDevice(this.Handle);
      device.OnSize(new System.Drawing.Size(300, 300));
      model = manager.CreateAutoCADModel();

      // you can use your block ref for this
      view = manager.CreateAutoCADView(new Line(new Point3d(0, 0, 0), new Point3d(300, 300, 0)));
      device.Add(view);
      view.ZoomExtents(new Point3d(0, 0, 0), new Point3d(300, 300, 0));

      //Bitmap bmp = view.GetSnapshot(new System.Drawing.Size(300, 300)) <<<<<------------------------
    }

    private void ViewFrm_Paint(object sender, PaintEventArgs e)
    {
      view.Invalidate();
      view.Update();
    }
  }
}


Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #28 on: September 21, 2010, 04:22:29 AM »
Finally I used that class without crashing
Once again Thank you

AHH!
It just crashed in the background


*EDIT*
 After I close the form about 5 to 10 seconds later it crashes


*EDIT*
http://forums.autodesk.com/t5/AutoCAD-2007/Unhandled-Access-Violaton-Writing-0x0014-Exception-at-7788fc47h/td-p/2674073
Is the same error
It might be something with my system
Thanks again for the code
« Last Edit: September 21, 2010, 04:42:39 AM by fro2001 »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #29 on: September 21, 2010, 05:52:58 AM »
Sorry, it was only indented to give you an idea, you probably need to do a bit of disposing     

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #30 on: September 21, 2010, 06:43:07 AM »
It was not your code

There is something wrong with my system

I have contacted ADN about it
And they sent some code to try and it crashed
I need to get back with them on it
Thanks again that will become very useful just not on this computer

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #31 on: September 21, 2010, 07:57:17 AM »
I get this when I call dispose...

Code: [Select]
private void ViewFrm_FormClosed(object sender, FormClosedEventArgs e)
    {
      view.EraseAll();
      device.EraseAll();
      manager.Dispose();
      view.Dispose();
      device.Dispose();
      line.Dispose();
    }

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #32 on: September 21, 2010, 08:45:27 AM »
I tried manager.Dispose(); and same thing
Writing to Vitural Address error

To show your form are you using either
Application.ShowModelessDialog() or  Application.ShowModalDialog();

Also what Command.Flags are you using for the command to open the form
In your command method what 

thanks


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Ex: Show Thumbnail in PictureBox VB.NET
« Reply #33 on: September 21, 2010, 08:49:10 AM »
just the default

Code: [Select]
[CommandMethod("doit")]
    static public void test()
    {
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      AcDb.Database database = HostApplicationServices.WorkingDatabase;
      try
      {
        ViewFrm frm = new ViewFrm();
        AcAp.Application.ShowModalDialog(frm);
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
      }
    }

the C++ stuff works like a charm of course it's a bit harder to code