2017-04-06 93 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