Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get the pixel color from a virtual device context ?

How to get the pixel color from a virtual device context ?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wangningyu
    wrote on last edited by
    #1

    I want to get some gdi elements color infomation. when I used TextOut function to draw the strings in the device context.but I doesn't want to output the strings in a really device context in the program's window. so I found this way :

    #include

    #pragma comment(lib,"user32.lib")

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pszCmdLine, int cmdShow)
    {
    // create a virtual dc to draw elements.
    HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
    HDC hdcCompatible = CreateCompatibleDC(hdcScreen);
    CString m_strText = _T("Hello");
    SIZE m_sz;

    if(hdcCompatible == NULL)
    {
        MessageBox(NULL,\_T("CreateCompatibleDC Failed !"),NULL,NULL);
        return -1;
    }
    
    // then set the text color is black,the back color is white.
    SetBkColor(hdcCompatible,RGB(255,255,255));
    SetTextColor(hdcCompatible,RGB(0,0,0));
    
    // draw the text int a virtual dc.
    TextOut(hdcCompatible,0,0,m\_strText,m\_strText.GetLength());
    
    // get the text width and height.
    GetTextExtentPoint32(hdcCompatible,m\_strText,m\_strText.GetLength(),&m\_sz);
    
    // get the pixel infomation.
    for(int i=0; i
    

    the m_sz value is normal ( 8 * 16),but the color variables is invalied, because is usually is the backcolor.

    ( I doesn't want to draw it in the program windows and then call GetPixel function from screen,that's not so good ! :) )

    why is it ,or is there any other way ?

    Thans for your reply !

    Best regards !

    G 1 Reply Last reply
    0
    • W wangningyu

      I want to get some gdi elements color infomation. when I used TextOut function to draw the strings in the device context.but I doesn't want to output the strings in a really device context in the program's window. so I found this way :

      #include

      #pragma comment(lib,"user32.lib")

      int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pszCmdLine, int cmdShow)
      {
      // create a virtual dc to draw elements.
      HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
      HDC hdcCompatible = CreateCompatibleDC(hdcScreen);
      CString m_strText = _T("Hello");
      SIZE m_sz;

      if(hdcCompatible == NULL)
      {
          MessageBox(NULL,\_T("CreateCompatibleDC Failed !"),NULL,NULL);
          return -1;
      }
      
      // then set the text color is black,the back color is white.
      SetBkColor(hdcCompatible,RGB(255,255,255));
      SetTextColor(hdcCompatible,RGB(0,0,0));
      
      // draw the text int a virtual dc.
      TextOut(hdcCompatible,0,0,m\_strText,m\_strText.GetLength());
      
      // get the text width and height.
      GetTextExtentPoint32(hdcCompatible,m\_strText,m\_strText.GetLength(),&m\_sz);
      
      // get the pixel infomation.
      for(int i=0; i
      

      the m_sz value is normal ( 8 * 16),but the color variables is invalied, because is usually is the backcolor.

      ( I doesn't want to draw it in the program windows and then call GetPixel function from screen,that's not so good ! :) )

      why is it ,or is there any other way ?

      Thans for your reply !

      Best regards !

      G Offline
      G Offline
      Gwenio
      wrote on last edited by
      #2

      Not sure (been a while since I used GDI), but maybe you should create a compatible bitmap and select it into the device context before drawing to it?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups