Home > Mobile >  C How to set pixel colors
C How to set pixel colors

Time:12-20

I created a code which change a certain pixels on the screen but when i want to change more pixels the performance of program will slow down. You will see glitches and it's not that pretty as it should be.

Question:
How can i inprove performance of the code.
If I want to change more pixel or eventually all pixels on the screen.
I thought about using SETBITMAPBITS but I'm not sure how to it works. I have no experience with it.

Is there any other solution?

Example of my code: < Console app >

#define _WIN32_WINNT 0x601
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    HDC dng = GetDC(NULL);
    
    while (true)
        for (int i = 0; i <= 200; i  )
            for (int j = 0; j <= 500; j  )
                SetPixel(dng, i, j, RGB(0, 0, 255));

    ReleaseDC(NULL, dng);
    getchar();
}

CodePudding user response:

If I understand correctly, you are trying to draw outside a window.

Every time you SetPixel you send a WM_PAINT message, which repaints the whole window. That dramatically slows down your program. What you should do is use GDI, GDI or Direct2D to create a bitmap or a rectangle to then draw it at once.

Drawing outside a window is never a good idea. You have no control on what you just drew, and it will disappear when something interrupts it.

If you want a blue block without a title bar, create a layered window, then create a rectangle and draw it.

Microsoft's documentation might not be a tutorial, but it is informative. You should check the documentation when trying to understand something.

Here is the Direct2D documentation: https://docs.microsoft.com/en-us/windows/win32/direct2d/getting-started-with-direct2d

And here is how to create a layered window: https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#layered-windows

Edit: Comment said that SetPixel doesn't send WM_PAINT. What I am saying is SetPixel repaints the window.

  • Related