【问题标题】:How to determine the dimensions of the icons in an icon object in .net, winforms?如何确定.net,winforms中图标对象中图标的尺寸?
【发布时间】:2010-01-15 19:58:09
【问题描述】:

我想知道在尝试之前是否可以验证图标所需的大小:

Dim myIcon = New Icon(theIcon, requestedSize).

这对于负数来说是失败的,所以这是一个简单的检查。

如果数字小于最小图标的一半,它似乎会掉下来。 我查看了图标类,但看不到任何提取尺寸的内容。

预计到达时间:

这很烦人。您可以放入 Int32.MaxValue 并选择最大的图标。为什么它不选择最小的图标,只要 size > 0 我不知道。 如果我可以确定最小图标的大小,那么我可以自己做——无需抛出异常。

预计到达时间:

这里有一些 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 的图标。

【问题讨论】:

    标签: .net winforms size icons validation


    【解决方案1】:

    您可能想看看这篇文章。看起来它触及了你正在寻找的东西,然后是一些。

    Code Project

    【讨论】:

    • 干杯,我正朝着那个方向前进,那篇文章应该可以为我节省一些工作。
    猜你喜欢
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 2015-06-11
    • 2012-11-18
    • 2012-03-06
    相关资源
    最近更新 更多