How to use SetPixel Function in Windows.h?
SetPixel() is a function that simply sets a pixel with the user-defined color. For Windows users, pixel operation can be easily done with C/C++ programming language. Here’s simple tutorial on “Using SetPixel Function in Windows.h”. Here, I’ve briefly introduced the need of pixels with program source code to setup SetPixel Function in Windows.h and the drawbacks of SetPixel() function.
While learning computer graphics, many times you might’ve come across creating graphical structure using pixels. To implement this in your program, a programming platform that supports pixel features is required. That is to say that the programming language, should provide the facility of printing a single pixel on the screen.
Now, let’s come to C/C++. Both support pixel operations, but you cannot print pixels in the console window. Some graphical structure mechanism is required for pixel operation in C/C++, and for Windows users, that mechanism is provided by windows.h library.
You can learn more about SetPixel function here.
Drawing Line with SetPixel
SetPixel() function, provided by windows.h can be used to print a pixel at any specified location of window. The general form of the function is: SetPixel(HDC hdc, int x, int y, COLORREF& color);
Here, hdc is a handle to a device context, x and y are coordinates of pixel and color is the color of the pixel to be displayed. To use SetPixel() function, besides writing the command for the function and including windows.h, extra linking operation need to done.
Source Code to Implement SetPixel Function in Windows.h:
The program code presented below draws a horizontal line to a window. Note that you need to add a link library: libgdi32.a in linker setting before running the code with Code::Blocks IDE. This link library can be found usually inside MinGW\lib.
#include static HWND sHwnd; static COLORREF redColor=RGB(255,0,0); static COLORREF blueColor=RGB(0,0,255); static COLORREF greenColor=RGB(0,255,0); void SetWindowHandle(HWND hwnd) < sHwnd=hwnd; >void setPixel(int x,int y,COLORREF& color=redColor) < if(sHwnd==NULL) < MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR); exit(0); >HDC hdc=GetDC(sHwnd); SetPixel(hdc,x,y,color); ReleaseDC(sHwnd,hdc); return; > void drawLine() < for(int i = 0; i < 100; i++) setPixel(10+i, 100, blueColor); >LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) < switch(message) < case WM_PAINT: SetWindowHandle(hwnd); drawLine(); break; case WM_CLOSE: // Failure to call DefWindowProc break; case WM_DESTROY: PostQuitMessage(0); return 0; default: break; // Failure to call DefWindowProc // >return DefWindowProc(hwnd,message,wParam,lParam); > int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow) < static TCHAR szAppName[] = TEXT("Test"); WNDCLASS wndclass; wndclass.style = CS_HREDRAW|CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; // Register the window if(!RegisterClass(& wndclass)) < MessageBox(NULL,"Registering the class failed","Error",MB_OK|MB_ICONERROR); exit(0); >// CreateWindow HWND hwnd=CreateWindow(szAppName,"SetPixel example - codewithc.com", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if(!hwnd) < MessageBox(NULL,"Window Creation Failed!","Error",MB_OK); exit(0); >// ShowWindow and UpdateWindow ShowWindow(hwnd,iCmdShow); UpdateWindow(hwnd); // Message Loop MSG msg; while(GetMessage(& msg,NULL,0,0)) < TranslateMessage(& msg); DispatchMessage(& msg); >return 0; > Limitations of SetPixel() Function:SetPixel() function seems easy but for very large image data it is very slow to work. So, for such cases, instead of using SetPixel or GetPixel, bitmap functions such as BlendFunction, TransparentBlt or BitBlt can be used. The basic coding for setting some pixels with specific color on the screen using SetPixel() function is:
int x[10000], y[10000]; for (int i=0; iThe problem here is that it accesses the device context for every single point individually. So, we should try to reduce accessing as well as writing in device context for every point and do more work in memory. This can done by creating a bitmap in memory. This acts as an alternative to SetPixel() function.
If you have any queries and feedback regarding the setup procedure aforementioned, bring them up from the comments section.
You Might Also Like Opening Files in Python: A Comprehensive Guide Python How to Read a File: Simplifying File Operations Creating Your First Program in C Language: A Beginner’s Guide A Beginner’s Guide to Finding the Domain and Range of a Function How to Format a String in Python: Techniques and Best Practices Share This Article What do you think? Leave a comment Leave a comment Leave a Reply Cancel reply Latest Posts Creating a Google Sheet to Track Google Drive Files: Step-by-Step Guide Blog June 23, 2024 Cutting-Edge Artificial Intelligence Project Unveiled in Machine Learning World Machine Learning Projects May 10, 2024 Enhancing Exams with Image Processing: E-Assessment Project Deep Learning Projects May 10, 2024 Cutting-Edge Blockchain Projects for Cryptocurrency Enthusiasts – Project Blockchain Projects May 10, 2024 Artificial Intelligence Marvel: Cutting-Edge Machine Learning Project Machine Learning Projects May 10, 2024 Code with C: Your Ultimate Hub for Programming Tutorials, Projects, and Source Codes” is much more than just a website – it’s a vibrant, buzzing hive of coding knowledge and creativity. Quick Link Top Categories- C Projects
- C++ Projects
- Python Projects
- ASP.NET Projects
- PHP Projects
- VB & VB.NET Projects
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Privacy & Cookies Policy Privacy OverviewThis website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Always EnabledNecessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessaryAny cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
Welcome Back!Sign in to your account