SetPixel() function on windows.h . How to use it??

If you are learning computer graphics, you may need to create graphical structure using pixels. When trying to implement this in program, you need the programming language that has facility to print a single pixel on the screen i.e. the language that supports pixel operation. Fortunately almost all Lagrange with GUI facility support pixel printing  Now what about C/C++? You cannot print a pixel in console window. You need some graphical mechanism for this. Fortunately, windows user can use windows.h library for this.
windows.h provides a function SetPixel() to print a pixel at specified location of a window. The general form of the function is

SetPixel(HDC hdc, int x, int y, COLORREF& color);

where, x and y are coordinates of pixel to be display and color is the color of pixel. To print a pixel only writing SetPixel command and embedding windows.h is not sufficient. You need some extra linking operation for this. You must link the library libgdi32.a in your IDE’s linker setting before using the SetPixel function. The following program draws a horizontal line to a window. If you have any confusion please ask me in the form of comment, I will reply you as soon as possible.


To run this code in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGWlib )  in linker setting.

SHARE SetPixel() function on windows.h . How to use it??

You may also like...

8 Responses

  1. Hey I can't get the SetPixel Function running using CodeBlocks 10.5 IDE with GNC compiler, can you give us a step by step tutorial on how to use it? thanks in advance.

  2. I meant GCC compiler, can you give us a brief tutorial on how to get it working with codeblocks please? thank you for those great tutorials

  3. Anonymous says:

    Wow thank you very much i was not quit sure how to use SetPixel correctly.

  4. Thank You Bibek!
    That piece of code is exactly what I need for my design.
    Made an empty project in VC Express, copied as main.c and it works perfectly.

  5. Isn't there a similar function called as 'putpixel()' from graphics.h? What's the difference between 'setpixel()' and 'putpixel()'?

  6. I get a syntax error on the last line ! any idea how to fix this ?

  7. Unknown says:

    I get a syntax error on the very last line when I try to compile! need help ! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *