2010-01-15 51 views
1

我想知道是否有可能試圖之前驗證一個圖標所需尺寸:如何確定圖標在.net,winforms中的圖標的尺寸?

Dim myIcon = New Icon(theIcon, requestedSize). 

這翻倒爲負數,所以這是一個簡單的檢查。

如果該數字小於最小圖標的一半,則會出現翻轉。 我看了一下圖標類,但沒有看到提取大小的內容。

ETA:

這很煩人。你可以放入Int32.MaxValue並選擇最大的圖標。爲什麼它不選擇最小的圖標,只要size> 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

回答

1

你可能想看看這篇文章的一個圖標。它看起來像涉及你在找什麼,然後是一些。

Code Project

+0

歡呼聲中,我在那個方向是標題,而文章應該救我一些工作。 – Jules 2010-01-15 21:39:10

相關問題