Hi, I'm trying to get the thumbnail from vault (2013). I found some code from Doug but can't get it to work. It fails on this line;
Dim im As Image = Image.FromStream(stream)
I can add/change the arguments (true, false) but that doesn't make a difference. It gives me an argument exception (A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll). What's wrong?
The code:
Dim PropInsts() As PropInst = PropSvc.GetProperties("FILE", New Long() {File.Id}, New Long() {ThumbProp.Id})
If Not PropInsts Is Nothing Then
If PropInsts(0).Val Is Nothing Then Return Nothing
Dim retVal As Image = Nothing
' convert the property value to a byte array
Dim thumbnailRaw As Byte() = DirectCast(PropInsts(0).Val, Byte())
' load the data into a stream but skip the first 12 bytes
Using stream As New IO.MemoryStream(thumbnailRaw, 12, thumbnailRaw.Length - 12)
Using br As New System.IO.BinaryReader(stream)
Dim CF_METAFILEPICT As Integer = 3
Dim CF_ENHMETAFILE As Integer = 14
Dim clipboardFormatId As Integer = br.ReadInt32()
Dim bytesRepresentMetafile As Boolean = (clipboardFormatId = CF_METAFILEPICT OrElse clipboardFormatId = CF_ENHMETAFILE)
Try
If bytesRepresentMetafile Then
' the bytes represent a clipboard metafile
' read past header information
br.ReadInt16()
br.ReadInt16()
br.ReadInt16()
br.ReadInt16()
Dim mf As New System.Drawing.Imaging.Metafile(br.BaseStream)
retVal = mf.GetThumbnailImage(width, height, New Image.GetThumbnailImageAbort(AddressOf GetThumbnailImageAbort), IntPtr.Zero)
Else
' the bytes do not represent a metafile, try to convert to an Image
stream.Seek(0, System.IO.SeekOrigin.Begin)
Dim im As Image = Image.FromStream(stream)
retVal = im.GetThumbnailImage(width, height, New Image.GetThumbnailImageAbort(AddressOf GetThumbnailImageAbort), IntPtr.Zero)
End If
Catch : End Try
End Using
End Using
Return retVal
End If
Solved! Go to Solution.
Solved by Redmond.D. Go to Solution.
I discovered that the problem lies here:
Using stream As New IO.MemoryStream(thumbnailRaw, 12, thumbnailRaw.Length - 12)
Remove the starting point and it works fine:
Using stream As New IO.MemoryStream(thumbnailRaw, 0, thumbnailRaw.Length)
The question remains; why? Does this work on every PC? I'm using win7/64bit...
The part that chops off the first 12 bytes of the MemoryStream is probably resulting in an invalid Image. I recently posted some updated code, so try that.