Thread: Display Images?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    19

    Display Images?

    I'm wondering if there is a way to cout an image. A very basic question, which I don't know exists, and very well may not exist. Thanks for any help

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Impossible.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    cout is for printing text to the screen. Nothing to do with pictures. cout an image doesn't make any sense, atleast to me.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Nope. The cout object wouldn't know how to handle an image. It wasn't designed for that. It was designed to be a output stream (or sequence if you like) of characters.

    You can however pretend you draw images with it...

    http://www.adrianxw.dk/SoftwareSite/...Consoles4.html
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Lol, try it! Read an image into a buffer and cout that to the screen. You'll get some interesting stuff but I'll guarantee it won't be a picture . -plays air guitar-
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well, you can overload operator<<
    Code:
    #include <windows.h>
    #include <iostream>
    
    class Bitmap
    {
    public:
      Bitmap(const char* file)
      {
        m_hImage = (HBITMAP)LoadImage(GetModuleHandle(0),file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
      }
      
      bool operator!() { return m_hImage==0?true:false; }
    
      void Draw()
      {
        HDC hdc = GetDC(GetConsoleWindow());
        BITMAPINFO bmi = {0};
        bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        if (!GetDIBits(hdc,m_hImage,0,0,0,&bmi,DIB_RGB_COLORS))
          std::cerr<<"Failed to get bitmap info";
       
        int width = bmi.bmiHeader.biWidth;
        int height = bmi.bmiHeader.biHeight;
        
        char* bits = new char[width*height*bmi.bmiHeader.biBitCount+sizeof(BITMAPINFOHEADER)];
        memcpy(bits, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
        
        if(!GetDIBits(hdc, m_hImage, 0, height, bits+bmi.bmiHeader.biSize, (LPBITMAPINFO)bits, DIB_RGB_COLORS))
          std::cerr<<"Failed to get bits: "<<GetLastError();
        
        if (!SetDIBitsToDevice(hdc,0,0,width,height,0,0,0,height,bits+bmi.bmiHeader.biSize,(LPBITMAPINFO)bits,DIB_RGB_COLORS))
          std::cerr<<"Failed to draw"<<GetLastError();
        ReleaseDC(GetConsoleWindow(),hdc);
      }
    private:
      HBITMAP m_hImage;
    };
    
    
    std::ostream& operator<<(std::ostream& os, Bitmap& image)
    {    
      image.Draw();
      return os;
    }
    
    
    
    int main()
    {
      Bitmap image("image.bmp");
      if(!image)
      {
        std::cerr<<"Failed to load image";
        return 1;
      }
      std::cout<<image;
      
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    mburt,

    The sad news is:
    There are no graphics, color, sound, mouse, or networking in Standard ANSI/ISO C++. Once you step outside of Standard C++, your code is no longer portable to other systems. (Or, at least not as-portable... There are cross-platform libraries.) Most real world programs contain non-standard code. That's why most commercial programs are only available for one platform!

    The good news is:
    If you're using a Windows compiler on a Windows system, all of that stuff is included in the WinAPI. It's fairly easy to display a bitmap. It get's a tiny bit trickier if you need to scale (re-size) the bitmap. If you want to display a JPEG, or other compressed image format, it has to be decoded first. That might require a CODEC or additional custom code.

    In general if your computer can do it, either your compiler can do it with it's included "extra" libraries, or your compiler can do it along with a 3rd-party library.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Well then. Now that everyone has rained on my parade....

    Okay, I'll see what I can do.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Code:
    #include <windows.h>
    #include <iostream>
    
    class Bitmap
    {
    public:
      Bitmap(const char* file)
      {
        m_hImage = (HBITMAP)LoadImage(GetModuleHandle(0),file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
      }
      
      bool operator!() { return m_hImage==0?true:false; }
    
      void Draw()
      {
        HDC hdc = GetDC(GetConsoleWindow());
        BITMAPINFO bmi = {0};
        bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        if (!GetDIBits(hdc,m_hImage,0,0,0,&bmi,DIB_RGB_COLORS))
          std::cerr<<"Failed to get bitmap info";
       
        int width = bmi.bmiHeader.biWidth;
        int height = bmi.bmiHeader.biHeight;
        
        char* bits = new char[width*height*bmi.bmiHeader.biBitCount+sizeof(BITMAPINFOHEADER)];
        memcpy(bits, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
        
        if(!GetDIBits(hdc, m_hImage, 0, height, bits+bmi.bmiHeader.biSize, (LPBITMAPINFO)bits, DIB_RGB_COLORS))
          std::cerr<<"Failed to get bits: "<<GetLastError();
        
        if (!SetDIBitsToDevice(hdc,0,0,width,height,0,0,0,height,bits+bmi.bmiHeader.biSize,(LPBITMAPINFO)bits,DIB_RGB_COLORS))
          std::cerr<<"Failed to draw"<<GetLastError();
        ReleaseDC(GetConsoleWindow(),hdc);
      }
    private:
      HBITMAP m_hImage;
    };
    
    
    std::ostream& operator<<(std::ostream& os, Bitmap& image)
    {    
      image.Draw();
      return os;
    }
    
    
    
    int main()
    {
      Bitmap image("image.bmp");
      if(!image)
      {
        std::cerr<<"Failed to load image";
        return 1;
      }
      std::cout<<image;
      
    }
    For some reason Dev-C++ won't compile this....

    It says:
    'GetConsoleWindow' undeclared (first use this function)

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    My bad. I accidentally left out the very first line:
    Code:
    #define _WIN32_WINNT 0x0500
    You'll need windows 2000 or later to run it, though
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Good lord... I can't seem to get this to work in Dev-C++.
    Here is what I have:

    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    #include <iostream>
    
    class Bitmap
    {
    public:
      Bitmap(const char* file)
      {
        m_hImage = (HBITMAP)LoadImage(GetModuleHandle(0),file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
      }
      
      bool operator!() { return m_hImage==0?true:false; }
    
      void Draw()
      {
        HDC hdc = GetDC(GetConsoleWindow());
        BITMAPINFO bmi = {0};
        bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        if (!GetDIBits(hdc,m_hImage,0,0,0,&bmi,DIB_RGB_COLORS))
          std::cerr<<"Failed to get bitmap info";
       
        int width = bmi.bmiHeader.biWidth;
        int height = bmi.bmiHeader.biHeight;
        
        char* bits = new char[width*height*bmi.bmiHeader.biBitCount+sizeof(BITMAPINFOHEADER)];
        memcpy(bits, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
        
        if(!GetDIBits(hdc, m_hImage, 0, height, bits+bmi.bmiHeader.biSize, (LPBITMAPINFO)bits, DIB_RGB_COLORS))
          std::cerr<<"Failed to get bits: "<<GetLastError();
        
        if (!SetDIBitsToDevice(hdc,0,0,width,height,0,0,0,height,bits+bmi.bmiHeader.biSize,(LPBITMAPINFO)bits,DIB_RGB_COLORS))
          std::cerr<<"Failed to draw"<<GetLastError();
        ReleaseDC(GetConsoleWindow(),hdc);
      }
    private:
      HBITMAP m_hImage;
    };
    
    
    std::ostream& operator<<(std::ostream& os, Bitmap& image)
    {    
      image.Draw();
      return os;
    }
    
    
    
    int main()
    {
      Bitmap image("image.bmp");
      if(!image)
      {
        std::cerr<<"Failed to load image";
        return 1;
      }
      std::cout<<image;
    }
    And it says:
    "undefined reference to GetDIBits"

  12. #12
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by JaWiB
    Well, you can overload operator<<
    Good one.

    >>"undefined reference to GetDIBits"<<

    You need to link with the gdi32 library: project menu --> project options, select the 'parameters' tab, and add -lgdi32 to the 'linker' field.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well I wasn't thinking 'good one' but it is a different type of example for operator overloading. Not sure I would use an operator like that, but hey the OP did ask if it was possible.

    Nice post Jawib.

  14. #14
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by Bubba
    Well I wasn't thinking 'good one'
    It amused me; 'twas an interesting thing to do with a DOS window and a clever answer to the question.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    We'll be cout-ing WAV files and MP3s next...

    But IMO this is all just fun curio, it just adds unneeded complexity into your code. Best to achieve this using standard function calls; it is faster, and simpler to comprehend and (very importantly) maintain.
    Last edited by jafet; 08-24-2006 at 10:35 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can i display images in C?
    By cursedninja in forum C Programming
    Replies: 7
    Last Post: 02-20-2009, 09:22 AM
  2. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  3. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. display pcx / bmp images
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-27-2001, 09:56 AM