2017-04-06 91 views
0

溢出使得當Windows窗體應用程序在vb.net中,我遇到了這個錯誤:算術運算導致與Color.FromArgb

System.OverflowException: 'Arithmetic operation resulted in an overflow.'

我的代碼:

Imports System.Runtime.InteropServices 

Public Class Form1 
    <DllImport("dwmapi.dll")> 
    Private Shared Sub DwmGetColorizationColor(ByRef ColorizationColor As UInteger, ByRef ColorizationOpaqueBlend As Boolean) 
    End Sub 

    Private Function UintToColor(ByVal argb As UInteger) 
     Dim a = argb >> 24 
     Dim r = argb >> 16 
     Dim g = argb >> 8 
     Dim b = argb >> 0 
     Return Color.FromArgb(a, r, g, b) 
    End Function 

    Dim windowColor 
    Dim windowBlend 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Me.Show() 
     DwmGetColorizationColor(windowColor, windowBlend) 
     Me.BackColor = UintToColor(windowColor) 
    End Sub 
End Class 

該函數返回(從 「汽車」):

一個:227 R:55182 克:14876783 b:3808456647 argb:3808456647

+0

'3808456647'是遠遠太大在'Int32'存儲。該API似乎與'System.Drawing.Color'類型不兼容。 –

+0

@SamAxe:奇怪的是,根據文檔,顏色值_應該被存儲爲「0xAARRGGBB」,就像'System.Drawing.Color'一樣。 –

+0

那麼,你有沒有嘗試改變你的數據類型爲Int32,只是爲了好玩? –

回答

0

我想你試圖將uint分成Byte大小的塊。

也許嘗試這個

Imports System.Runtime.InteropServices 
Public Class Form1 
    <DllImport("dwmapi.dll")> 
    Private Shared Sub DwmGetColorizationColor(ByRef ColorizationColor As UInteger, ByRef ColorizationOpaqueBlend As Boolean) 
    End Sub 

    Private Function UintToColor(ByVal argb As UInteger) 



     Dim bytes As Byte() = BitConverter.GetBytes(argb) 


     Dim b = bytes(0) 
     Dim g = bytes(1) 
     Dim r = bytes(2) 
     Dim a = bytes(3) 



     Dim result As Color = Color.FromArgb(a, r, g, b) 
     Return result 
    End Function 

    Dim windowColor 
    Dim windowBlend 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Me.Show() 
     DwmGetColorizationColor(windowColor, windowBlend) 
     Me.BackColor = UintToColor(windowColor) 
    End Sub 
End Class