Solved

Load an image into a bitmap

Posted on 1998-06-08
7
371 Views
Last Modified: 2013-12-03
I have some raw 8-bit grayscale data that I want to load into a bitmap.  Can anyone give me directions
   on how to load this data and display it in a CFormView.  I'm using VC5.0 and MFC. Thanx

   zeekial@yahoo.
0
Comment
Question by:zeekial
  • 4
  • 2
7 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1316195
You need to convert the raw 8-bit grayscale data into a DIB.

1. Read the data into memory.
2. Create a grayscale palette.
3. Form the data and the palette to a DIB.
4. Display it.

Look into the MFC sample "DIBLOOK: Illustrates the Use of DIBs and Color Palettes" and the Win32 sample "Mypal: Palette Management Example".
0
 

Author Comment

by:zeekial
ID: 1316196
Here's some code to show you where I stand:

      int fh;
      char buffer[60000];
      unsigned int nbytes = 60000;

      fh = open("imagefile",O_RDONLY)
      read(fh, buffer, nbytes);

      now all my data is in character buffer "buffer".  How do I make a grayscale palette and how do I put this buffer into a DIB and display?  Which functions do I use?  
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 200 total points
ID: 1316197
You can simply create a compatible bitmap of the appropriate size, create a DC for it, and then use a loop that does a SetPixel for each pixel in the drawing setting its RGB values to be the same.

You don't _need_ to create a palette .. only if your dispaly has a limited number of colours (<16bit colour say) and you want to see all your grey scale levels.  Without a palette, windows will map your colours to the nearest colour available in the current palette.

0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1316198
SetPixelV is quicker, BTW than SetPixel

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1316199
try something like this ...

  CBitmap bitmap;
  CDC dcMem;
  dcMem.CreateCompatibleDC(NULL);
  bitmap.CreateCompatibleBitmap (hdc, width, height);
  CBitmap* pbitmapOld = dcMem.SelectObject(&bitmap);
  int i = 0;
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      char grey = buffer[i++];
      dcMem.SetPixelV (x,y,RGB(grey,grey,grey));
    }
  }
  dcMem.SelectObject(pbitmapOld);

0
 
LVL 23

Expert Comment

by:chensu
ID: 1316200
>You don't _need_ to create a palette .. only if your dispaly has a limited number of colours (<16bit colour say) and you want to see all your grey scale levels.

Nowadays, there are still a lot of systems working on 256-color mode.

>Without a palette, windows will map your colours to the nearest colour available in the current palette.

Yes. In this case, the display will be likely looking ugly since you cannot guarantee the current palette contains enough grayscale entries (RGB(x, x, x)).

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1316201
The code above can be changed to create a true colour DIB (no palette required CreateDIBSection) and the set the pixels.  The palette manipulation can then be done at display time.

Alternatively, create a compatible bitmap (as above) and a use palette.

NOTE: If using CreateDIBSection, you can get back a points to the array of pixel values, and set them diretcly without using SetPixelV.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hibernate methods 2 72
Printout, or copy to a file,  thumbnails of ALL available fonts 5 76
how to split multiple lines delimiter : 8 99
Capture logon name 13 73
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question