Calculate the color of GDI hpalette - GDI+ secrets
Private Function GetHppaletteColor() As Color
Return Image.FromHbitmap(New Bitmap(Me.Width, Me.Height).GetHbitmap(), IntPtr.Zero).GetPixel(0, 0)
End Function
or
Private Function GetHppaletteColor() As Color
Return Image.FromHbitmap(New Bitmap(Me.Width, Me.Height).GetHbitmap(), Graphics.FromHwnd(IntPtr.Zero).GetHdc).GetPixel(0, 0)
End Function
the below code shows hoe to save the screen bitmap which hold the hpalette color
Dim rect As Rectangle = CType(Nothing, Rectangle)
Dim devices As Screen() = Screen.AllScreens
For i As Integer = 0 To devices.Length - 1
Dim device As Screen = devices(i)
rect = Rectangle.Union(rect, device.Bounds)
Next
Dim mask As Bitmap = Image.FromHbitmap(New Bitmap(rect.Size.Width, rect.Size.Height).GetHbitmap(), Graphics.FromHwnd(IntPtr.Zero).GetHdc())
Dim fileName As String = Application.StartupPath & "\screen.jpg"
mask.Save(fileName)
' Test Control
Public Class DominantControl
Inherits Control
Public Sub New()
MyBase.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)
Dim rect As Rectangle = New Rectangle(0, 0, MyBase.Width, MyBase.Height)
Using sb As New SolidBrush(GetHppaletteColor())
pevent.Graphics.FillRectangle(sb, rect)
End Using
End Sub
Private Function GetHppaletteColor() As Color
Dim rect As Rectangle = CType(Nothing, Rectangle)
Dim devices As Screen() = Screen.AllScreens
For i As Integer = 0 To devices.Length - 1
Dim device As Screen = devices(i)
rect = Rectangle.Union(rect, device.Bounds)
Next
Return Image.FromHbitmap(New Bitmap(rect.Width, rect.Height).GetHbitmap(), Graphics.FromHwnd(IntPtr.Zero).GetHdc).GetPixel(0, 0)
End Function
End Class
build the control, add to form and notice what will happen when you try to change the control backcolor
Reference
Comments
Post a Comment