CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2006
    Posts
    1

    Simple bitmap program

    Hello everyone, I have Visual C++ and basic understanding of programming. I enjoy tinkering with simple programs but often run into trouble getting them to work. Recently I have been trying to make a program to read from a bitmap file. What I actually want to do is use the pixel information from a bitmap image to create ASCII text art. I think I may be able to create the text output with what I already know, but I am unsure how to actually find the pixel values for the bits in a bitmap file. So far, all I have been able to do is load up the file with an input stream with something like this:

    #include "stdafx.h"
    #include <iostream>
    #include <fstream>

    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
    ifstream is("myBitmap.bmp");

    system("pause");
    return 0;
    }


    I've tried using the cout command and some other commands that i'm not all that fammiliar with such as is.get() to try to find pixel values. However, so far i've only been able to get it to display the file address and some values that appear to be garbage. If anyone could provide me with some simple code to find bitmap pixel values I would appreciate it. Thanks

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Simple bitmap program

    Reading bitmap files requires a bit more parsing of header information. See MSDN for more information.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Oct 2002
    Location
    Italy
    Posts
    324

    Re: Simple bitmap program

    You have to create a dibsection from your bitmap; then you can access the bits directly, as in the following example:

    HBITMAP hBitmap = (HBITMAP) ::LoadImage(NULL, "myBitmap.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);

    if (hBitmap)
    {
    BITMAP bm;
    ::GetObject(hBitmap, sizeof(BITMAP), &bm);

    if (bm.bmBits)
    {
    // access the bitmap bits here,
    // be sure to check the format first
    }
    }


    Please check the following links, as they can be of interest:
    http://libcaca.zoy.org (this seems to be temporarily down)
    http://aa-project.sourceforge.net/gallery
    Regards,
    Marco Era
    www.marcoera.com

    Latest post on my blog: Back to the Amiga's times

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured