Can anyone tell me where I'm going wrong with the following code? Regardless of the valid img and ID I pass, it will only return a single double quote and the first character of the string.
Thanks,
Mike
Private Function PropertyItemValueToString(ByVal img As System.Drawing.Image, ByVal ID As Integer) As String
Are you sure the property you are extracting is type text? Maybe it's an integer.
You can look at all the properties like this:
Dim img As System.Drawing.Image = Bitmap.FromFile(MapPath("~\images\april.jpg"))
For Each pi As PropertyItem In img.PropertyItems
Response.Write(pi.Id + ", " + pi.Len + ", " + pi.Type + "<br />")
Next
Once you know the type, you should be able to figure out how to extract it to a .Net type.
which almost represents the string "title file property" (except for the zero values it seems), which is what's in the title field on the summary tab of the file I'm working with.
Everything that I've used to try and get the string just returns the first character except this:
Private Function PropertyItemValueToString(ByVal img As System.Drawing.Image, ByVal ID As Integer) As String
Dim pi As System.Drawing.Imaging.PropertyItem
pi = img.GetPropertyItem(ID)
Dim str As String = Nothing
For i As Integer = 0 To pi.Value.Length - 1
If pi.Value(i) <> 0 Then
str += Chr(pi.Value(i))
End If
Next
Return str
End Function
which just loops through the array, skips the zero values and converts the rest to individual characters. I mean, it works but I can't believe there's not some kind of "getchars" or "getstring" that will do this better.
Member
4 Points
18 Posts
propertyitem to string
Jan 16, 2011 09:19 PM|mikeharness|LINK
Hello,
Can anyone tell me where I'm going wrong with the following code? Regardless of the valid img and ID I pass, it will only return a single double quote and the first character of the string.
Thanks,
Mike
Private Function PropertyItemValueToString(ByVal img As System.Drawing.Image, ByVal ID As Integer) As String
Dim pi As System.Drawing.Imaging.PropertyItem
pi = img.GetPropertyItem(ID)
Dim ascii As Encoding
ascii = Encoding.ASCII
Dim str As String
str = ascii.GetString(pi.Value, 0, pi.Len - 1)
Return str
End Function
All-Star
124328 Points
10142 Posts
Re: propertyitem to string
Jan 16, 2011 11:55 PM|SGWellens|LINK
Are you sure the property you are extracting is type text? Maybe it's an integer.
You can look at all the properties like this:
Once you know the type, you should be able to figure out how to extract it to a .Net type.
My blog
Member
4 Points
18 Posts
Re: propertyitem to string
Jan 17, 2011 11:02 AM|mikeharness|LINK
Thanks Steve.
I'm honestly not sure what it is--a byte array I suppose.
Here's what's in PropertyInformation ID 40091 for the file I'm looking at:
ID: 40091
Len: 40
Type: 1
Value: array
(0) 116
(1) 0
(2) 105
...
(36) 121
(37) 0
(38) 0
(39) 0
which almost represents the string "title file property" (except for the zero values it seems), which is what's in the title field on the summary tab of the file I'm working with.
Everything that I've used to try and get the string just returns the first character except this:
which just loops through the array, skips the zero values and converts the rest to individual characters. I mean, it works but I can't believe there's not some kind of "getchars" or "getstring" that will do this better.
Any other ideas?
Mike
All-Star
124328 Points
10142 Posts
Re: propertyitem to string
Jan 17, 2011 12:53 PM|SGWellens|LINK
It's ExIf information. Here's how to get the author:
Dim AuthorItem As PropertyItem = img.GetPropertyItem(40093)
Dim Author As [String] = Encoding.Unicode.GetString(AuthorItem.Value)
My blog
Member
4 Points
18 Posts
Re: propertyitem to string
Jan 17, 2011 02:36 PM|mikeharness|LINK
Excellent! That works perfectly. Thanks very much Steve. Mike