如何确定.net、winforms中图标对象中图标的尺寸?
我想知道在尝试之前是否可以验证图标所需的大小:
Dim myIcon = New Icon(theIcon, requestedSize).
这会导致负数,所以这是一个简单的检查。
如果数字小于最小图标的一半,则看起来会倒塌。 我查看了图标类,但看不到任何用于提取尺寸的内容。
ETA:
这很烦人。您可以输入 Int32.MaxValue ,它会选择最大的图标。为什么它不选择最小的图标,只要尺寸>即可0 我不知道。 如果我可以确定最小图标的大小,那么我可以自己做到这一点 - 无需抛出异常。
ETA:
这里有一些 VB 代码供感兴趣的人使用:
//Returns an array of IconMetaData which contains, amongst other things, the size of
// each image in the icon.
<Extension()> _
Public Function GetMetaData(ByVal icon As Icon) As IconMetaData()
Using s As New System.IO.MemoryStream
icon.Save(s)
Using r As New BinaryReader(s)
s.Position = 0
Dim Header As New IconHeader(r)
Dim Data As New List(Of IconMetaData)
For i As Integer = 0 To Header.NumberOfIcons - 1
Dim d As New IconMetaData(r)
*See note below.
If d.Height <> 0 AndAlso d.Width <> 0 Then
Data.Add(d)
End If
Next
Return Data.ToArray
End Using
End Using
End Function
Private Class IconHeader
Public ReadOnly NumberOfIcons As Short
Public Sub New(ByVal r As BinaryReader)
r.ReadInt16() //Reserved
r.ReadInt16() //Type, 0=Bitmap, 1=Icon
Me.NumberOfIcons = r.ReadInt16
End Sub
End Class
Public Class IconMetaData
Public ReadOnly Width As Byte
Public ReadOnly Height As Byte
Public ReadOnly ColorCount As Byte
Public ReadOnly Planes As Short
Public ReadOnly BitCount As Short
Friend Sub New(ByVal r As BinaryReader)
Me.Width = r.ReadByte
Me.Height = r.ReadByte
Me.ColorCount = r.ReadByte
r.ReadByte() //Reserved
Me.Planes = r.ReadInt16
Me.BitCount = r.ReadInt16
r.ReadInt32() //Bytes in res
r.ReadInt32() //Image offset
End Sub
End Class
*注意:从我测试过的几个图标来看,第一个条目的尺寸为 (0,0)。我不知道为什么,而且我不能确定所有图标都有这个条目,或者它总是第一个。因此,我会逐一检查。
ETA:经过进一步调查,我发现 0 用于指示大小为 256 的图标。
I was wondering whether it's possible to validate a required size for an icon before trying:
Dim myIcon = New Icon(theIcon, requestedSize).
This falls over for negative numbers, so that's an easy check.
It appears it falls over if the number is less than half of the smallest icon.
I've looked at the icon class but can see nothing for extracting the sizes.
ETA:
This is pretty annoying. You can put Int32.MaxValue in and it chooses the largest icon. Why it doesn't choose the smallest icon, as long as size > 0 I don't know.
If I can determine the size of the smallest icon, then I can do that myself - with no need to throw an exception.
ETA:
Here's some VB code for anyone who's interested:
//Returns an array of IconMetaData which contains, amongst other things, the size of
// each image in the icon.
<Extension()> _
Public Function GetMetaData(ByVal icon As Icon) As IconMetaData()
Using s As New System.IO.MemoryStream
icon.Save(s)
Using r As New BinaryReader(s)
s.Position = 0
Dim Header As New IconHeader(r)
Dim Data As New List(Of IconMetaData)
For i As Integer = 0 To Header.NumberOfIcons - 1
Dim d As New IconMetaData(r)
*See note below.
If d.Height <> 0 AndAlso d.Width <> 0 Then
Data.Add(d)
End If
Next
Return Data.ToArray
End Using
End Using
End Function
Private Class IconHeader
Public ReadOnly NumberOfIcons As Short
Public Sub New(ByVal r As BinaryReader)
r.ReadInt16() //Reserved
r.ReadInt16() //Type, 0=Bitmap, 1=Icon
Me.NumberOfIcons = r.ReadInt16
End Sub
End Class
Public Class IconMetaData
Public ReadOnly Width As Byte
Public ReadOnly Height As Byte
Public ReadOnly ColorCount As Byte
Public ReadOnly Planes As Short
Public ReadOnly BitCount As Short
Friend Sub New(ByVal r As BinaryReader)
Me.Width = r.ReadByte
Me.Height = r.ReadByte
Me.ColorCount = r.ReadByte
r.ReadByte() //Reserved
Me.Planes = r.ReadInt16
Me.BitCount = r.ReadInt16
r.ReadInt32() //Bytes in res
r.ReadInt32() //Image offset
End Sub
End Class
*Note: From the couple of icons i've tested this with, the first entry has dimensions of (0,0). I don't know why, and I can't be sure that all icons have this entry, or that it's always the first. Therefore, I check each one.
ETA: On further investigation, i've found that 0 is used to indicate an icon of size 256.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想看一下这篇文章。看起来它涉及到你正在寻找的东西,然后是一些。
代码项目
You might want to take a look at this article. It looks like it touches on what you are looking for and then some.
Code Project