0

When making a Windows Forms Application in vb.net, I ran into this error:

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

My code:

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

The function returns (from "autos"):

a: 227 r: 55182 g: 14876783 b: 3808456647 argb: 3808456647

Nanomotion
  • 115
  • 1
  • 1
  • 10
  • `3808456647` is far too big to store in an `Int32`. That API does not appear to be compatible with the `System.Drawing.Color` type. – Sam Axe Apr 06 '17 at 22:38
  • @SamAxe : The strange thing is that according to the documentation the color value _should_ be stored as `0xAARRGGBB`, just like `System.Drawing.Color`. – Visual Vincent Apr 06 '17 at 22:44
  • Well, have you tried changing your data type to Int32, just for fun? – Sam Axe Apr 06 '17 at 22:48
  • Apparently the color is stored in a registry key, so you could try getting that instead: http://stackoverflow.com/a/13087542/3740093 – Visual Vincent Apr 06 '17 at 22:57
  • The numbers are getting increasingly larger because you're shifting less bits into it each time. essentially your cropping a few bits off of your integer, then a few more, then a few more. – ThatGuy Apr 06 '17 at 23:22

1 Answers1

0

I think you're trying to split the uint into Byte sized chunks.

Maybe try this

    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
ThatGuy
  • 228
  • 1
  • 12