I want to reduce the time it takes to get RGB color for pixels (pixels) at a specified location.

Asked 2 years ago, Updated 2 years ago, 77 views

[Visual Studio, VB] I want to shorten the time it takes to get RGB colors for pixels (pixels) at the specified location

Creating Windows form applications in Visual Studio 2017 Community.

The pixel color at the mouse pointer position is set to the background color of PictureBox shutsuryoku, and R(0-255), G(0-255), and B(0-255) are displayed in Label ontei, onlyou, joutai, respectively.

I use PictureBox, but I don't handle images such as bitmaps.You can read colors freely at any point on the display.

Here is the source code:It's a program I'm just making, so don't worry about some pointless procedures.

Public Class Form 1

    ' ■ API function declaration
    Declare Sub Sleep Lib "kernel32" (ByValdwMillisconds As Integer)

    'Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Long

    Declare Function GetTickCount Lib "kernel32" ( ) As Long

    Declare Function GetPixel Lib "gdi32" Alias "GetPixel"_
        (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As UInt32

    Declare Function GetDC Lib "user32.dll" (ByVal hWnd As Integer) As Integer

    Declare Function ReleaseDC Lib "user32" (ByVal hwnd As_)
        IntPtr, ByVal hdc As IntPtr) As UInt32

    Declare Function GetDesktopWindow Lib "user32"() As Long

    ' ■ declaration of variable
    Private i As Long
    Private j As Long
    Private As Long
    Private tick As Long
    Private window As Long
    Private hdc As Long
    Private Pixel Color As Color
    Private Speed Update Time As Long
    Private pointer position X As Long
    Private pointer position YAs Long

    Private Output Color As Color

    US>'What to do when the form is loaded?
    Private SubForm1_Load (sender As Object, eAs EventArgs) Handles MyBase.Load

        'Get the Desktop Window Handle
        window=GetDesktopWindow

        'Get DC handle
        hdc = GetDC (window)


        tick = 0
        Speed Update Time = GetTickCount

    End Sub

    Private Sub Timer 1_Tick (sender As Object, eAs EventArgs) Handles Timer 1.Tick

        ' ■ Count the tick count
        tick=tick+1

        ' ■ Display update speed
        If tick Mod 10 = 0 Then
            If GetTickCount - Speed Update Time > 0 Then
                koushinsokudo.Text=Int(1/(GetTickCount - Speed Update Time)/10/1000))
            End If
            Speed Update Time = GetTickCount
        End If

        ' ■ Get pointer position
        Pointer Location X = System.Windows.Forms.Cursor.Position.X
        Pointer Location Y=System.Windows.Forms.Cursor.Position.Y

        'View
        pX.Text = pointer position X
        pY.Text = pointer position Y

        ' ■ Get pointer position color
        Forn=1 To 6
            Get Call pixel color (pointer position X, pointer position Y)
        Next
        'RGB value converted to Color type
        Output Color = Pixel Color

        'Output
        shutsuryoku.BackColor= Output Color

        US>'View RGB values
        ontei.Text=pixel color.R
        onlyyou.Text=pixel color.G
        joutai.Text = Pixel color.B

        US>' Displayed in Progress Bar
        US>'To disable the display of animation in which the bar gradually grows when the value increases
        'Reduce from a larger value
        onteibar.Maximum=256
        onteibar.Value=256
        onteibar.Value=pixel color.R
        onteibar.Maximum=255

        onryoubar.Maximum=256
        onryoubar.Value=256
        onryoubar.Value=pixel color .G
        onlyoubar.Maximum=255

        joutaibar.Maximum=256
        joutaibar.Value = 256
        joutaibar.Value = Pixel color .B
        joutaibar.Maximum=255

    End Sub
    Obtain subpixel color (ByVal x As Long, ByValy As Long)

        Dim Color As Long


        'Get Pixel Colors
        Color=GetPixel(hdc,x,y)



        US>'Convert Long to Color
        Pixel Color = ColorTranslator.FromOle (Color)

    End Sub

    When the Find Location button is pressed,
    Private Sub Button 1_Click (sender As Object, eAs EventArgs) Handles Button 1.Click



    End Sub

    US>'When closing the form
    Private SubForm1_FormClosing(ByVal sender As Object,
        ByVale As System.Windows.Forms.FormClosingEventArgs)Handles Me.FormClosing

        US>'Release
        ReleaseDC (window, hdc)

    End Sub
End Class

There are no compilation or runtime errors, but it takes a lot of time to get the color of the pixels.

The procedure Timer1_Tick runs only about nine times per second.
(Label koushinsokudo shows how many times Timer1_Tick has been run per second.)

Forn=1 To 6
    Get Call pixel color (pointer position X, pointer position Y)
Next

As for the part of , it doesn't make sense to repeat the same process six times with this program, but as I mentioned earlier, this program is still in the process and I will try to do six meaningful "acquire pixel colors" when it is completed.

Therefore, we will have to run at least six "Find Pixel Colors" within Timer1_Tick, so we will write about these programs.

By the way, if you rewrite Forn=1To6 to Forn=1To1, it will be about 60 times per second.Therefore, it is unlikely that the processing other than color acquisition is taking an unusual amount of time.

The specifications of the computer are as follows.
OS Windows 10 Home CPU 2.50 GHz (Core i5-3210M)
RAM 8 [GB]

The system type is 64-bit operating system, x64-based processors.

When you run this program, use the

  • This program
  • Visual Studio 2017 Community
  • Task Manager

Other applications are not running at the same time.

Also, if you check the CPU utilization in Task Manager, it is less than 10 percent, so it is unlikely that the background process is slowing down this program.

Now I have a question, how do I reduce the time it takes to get a pixel color?Please let me know if there is a way to do so.

Ideally, Timer1_Tick should run more than 40 times per second (240 or more color acquisition), but if that's not possible, it can be 15 or 20 times per second.

visual-studio vb.net

2022-09-30 15:01

1 Answers

It is not directly related, but GetPixel is very slow, which is causing MFC Ribbon display problems reports.The GetPixel speed may improve in the future.


2022-09-30 15:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.