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

0 Members and 2 Guests 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: 8728
  • 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