none
Vb.net How can i Adjust a screen monitor contrast using i Trackbar ?

    Question

  • Option Strict On
    Imports System.Drawing.Imaging

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True

            With TrackBar1
                .Maximum = 255
                .Minimum = 1
                .Value = 1
            End With
        End Sub
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            Dim clr1 As Color = Color.Black
            Dim clr2 As Color = Color.White

            Dim r, g, b As Integer

            r = CInt(clr1.R + (CInt(TrackBar1.Value) * (clr2.R - clr1.R)) / 255)
            g = CInt(clr1.G + (CInt(TrackBar1.Value) * (clr2.G - clr1.G)) / 255)
            b = CInt(clr1.B + (CInt(TrackBar1.Value) * (clr2.B - clr1.B)) / 255
            e.Graphics.Clear(Color.FromArgb(200, r, g, b))

        End Sub

        Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
            Invalidate()
        End Sub
    End Class
    • Edited by Jay Rou Thursday, March 16, 2017 9:17 AM
    Thursday, March 16, 2017 9:11 AM

All replies

  • Hi Jay,

    Based on your description, you want to adjust a screen monitor contrast using Trackbar, please find an API on MSDN and  add Imports System.Runtime.InteropServices firstly, you can refer to the sample below.

    Code sample:

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
        Private Structure RAMP
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)>
            Public Red As UInt16()
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)>
            Public Green As UInt16()
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)>
            Public Blue As UInt16()
        End Structure
    
        <DllImport("gdi32.dll")>
        Private Shared Function SetDeviceGammaRamp(hDC As IntPtr, ByRef lpRamp As RAMP) As Boolean
        End Function
    
        <DllImport("user32.dll")>
        Private Shared Function GetDC(hWnd As IntPtr) As IntPtr
        End Function
        ''' <summary> Required designer variable.</summary>
        Private components As System.ComponentModel.IContainer = Nothing
    
    
    
        Private Shared s_ramp As New RAMP()
        Public Sub New()
            InitializeComponent()
        End Sub
    
    
        Protected Overrides Sub Dispose(disposing As Boolean)
            If disposing AndAlso (components IsNot Nothing) Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        Private Shared Sub SetGamma(gamma As Integer)
            s_ramp.Red = New UShort(255) {}
            s_ramp.Green = New UShort(255) {}
            s_ramp.Blue = New UShort(255) {}
    
            For i As Integer = 1 To 255
                ' gamma is a value between 3 and 44
                s_ramp.Red(i) = InlineAssignHelper(s_ramp.Green(i), InlineAssignHelper(s_ramp.Blue(i), CUShort(Math.Min(65535, Math.Max(0, Math.Pow((i + 1) / 256.0, gamma * 0.1) * 65535 + 0.5)))))
            Next
            ' Now set the value.
            SetDeviceGammaRamp(GetDC(IntPtr.Zero), s_ramp)
        End Sub
    
        Private Sub trackBar1_ValueChanged(sender As Object, e As EventArgs)
            Try
                SetGamma(trackBar1.Value)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    

    Hope it is helpful to you.

    Best regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 17, 2017 7:56 AM
    Moderator
  • SetDeviceGammaRamp(GetDC(IntPtr.Zero), s_ramp)

    I'm pretty sure this causes a memory leak...  I think you need to call ReleaseDC on a screen device context.  You can probably use one DC handle for the lifetime of the application - if the call ever fails you can get a new DC.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, March 18, 2017 12:52 AM
    Moderator