Link to home
Start Free TrialLog in
Avatar of rampriya_sb
rampriya_sb

asked on

Displaying a Bitmap Image, where datas are in byte array

Hi,

I need to display the BMP image on the screen (dialog based) using VC++. I have the image data in a byte array, so can any one help me out in displaying this image. Is there any API for this?? i m bit new to C++, so please help me out.

Thanks in advance
Ram
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Your question is in the wrong area: Even though you are programming in C++, it's about a Windows programming problem. Depending on how you create your application (Windows SDK, MFC, ...) you should post a request in Community Support to move the question.

Does the image have to stay in the byte array, or would it be possible to store the bitmap in a resource file? If that's possible, your problem just got a lot simpler. In case you are using MFC, add your image to the resource file, then place a picture control on your dialog. Bring up preferences for the picture control and select your image.
Use DIB Functions

SetDIBits, StretchDIBits

Good Luck
ASKER CERTIFIED SOLUTION
Avatar of RJSoft
RJSoft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rampriya_sb
rampriya_sb

ASKER

Hi,

Thanks a lot for the help, This is great. But since i m new to C++, it will be nice if you can send me a small project code regarding this. This will help me a lot. you can mail me at rampriya_sb@yahoo.com

I appreciate it.

Thanks
Ram
Avatar of DanRollins
rampriya_sb,
EE does not work well when Experts and Askers correspond by email.  In fact, it is against the rules except in very rare cases.

You have not responded to the questions posed by the Experts and you must do so in order for anyone to provide code for you.  Please describe the format of the image data that you have.  Then one of us can provide code that will put that image data into a bitmap and show you how to display it.

-- Dan
I m sorry about this, i'll take care not to repeat it again. As i was new to C++, i wanted someone to help me out with this in detail.

Well, i have got my solution solved. Thanks

Ram
I was un-aware of that rule also. But it makes sense.

Here is the email coorispondance in reguard to this subject

BEGIN EMAIL/////////////////////////////////////////
Hello Rampriya;

I assume your using VC++ 6.0. We need to establish
this first.

Assuming you are.

All you really need to do is copy these steps I am
going to tell you. It took me about 10 minutes to
write and compile the code. Take these steps.

First create a dialog based application using MFC
Appwizard (exe)

Select whatever directory and name of app you like.

Select Dialog based app.

You can remove the check from About box and ActiveX
controls if you like dont matter. I like to leave the
3d controlls because it looks better.

Press finish and watch as the classes and resource etc
are made for you.

Next if your not already in the resource editor then
open it up by selecting ResourceView.

When you create a Dialog based app the main window is
a dialog. The VC++6.0 version of a basic dialog based
application comes with a static text box, it has the
text "TODO: Place dialog controls here."

"Right Click" on that text because that is going to be
a box where you can display your image. Select
Properties and change the default IDC_STATIC to
IDC_PAINTBOX or whatever you like. Select the tab
Styles and check the box sunken. The sunken property
will help you when your sizing the static text box so
you can see it.

Next size the static text box so that it is a few
inches square. Whatever size you want but make it a
little larger than a text box so you can see the
picture when it is displayed in the box.

Now you have a static box with the ID of IDC_PAINTBOX
if you copied me. Next you need to assign a variable
to it. But before you do that you need to create your
own personalized static class with the base class
CStatic. Dont worry it's easy.

In Classview (use tab at bottom left of VC) add a new
class by "right clicking" the first class on the top
of your class listing. Select New class.

In the Name box name the class MyStatic. Then for the
base class scroll down and choose CStatic. Automation
leave as none. Press Ok.

Now you have your customized Static class it is called
MyStatic.

Now go back to Resource View (bottom left tabs of VC)
so you can see your Dialog box with the static
control.

Right click anywhere on the Dialog (not control)
Select ClassWizard.

BTW, ClassWizard IS GREAT! Once you get the hang of it
I know you will love it.

Once in Classwizard select the Members Variable tab.
Then select your static control ID and add a variable.

I like to keep the leading m_ because it is the
default and later it helps you see your control
variables quickly.

Now your in the dialog of Add Member Variable. You
want to create a control type variable. Name your
varialbe something like m_PaintBox. Use Control and
scroll down to you see your customized class MyStatic.
Press Ok.

A dialog comes up that says you screeweed up!!!! No
only joking. It is only a warning to remind you to
include your "MyStatic.h" file which you have to do
manually.

So I go to file view and open up the main window
dialog header file. ( A .h file with the name of your
application and a Dlg in it) So if you named your app
JuneBug then it would be JuneBugDlg.h

You can add your include at the top of the .h file
#include "MyStatic.h"

Now you have a class called MyStatic which is your
customizable subclass of the base class CStatic and
you have an instance variable called m_PaintBox that
is owned by your main window dialog. This object
m_PaintBox is also tied to your dialog by the resource
Id IDC_PAINTBOX.

Now you need to add your customization to your
customized static class MyStatic. So back in class
view (bottom left of VC tab). Select Class View.

Open up your MyStatic Class so we can add a function
to it.

"Right Click" on the top MyStatic and select Add
Member Function.

For the function type you want to type in the word
void. I know this is a little goofy because they imply
that the function is a void type, but what they mean
to say is what type of return is it. Next type in the
name of your function MyPaint() and press enter. You
should keep the function as public. (Dont worry about
that for now dont matter, private is just for big
projects where multiple persons are programming using
your class so you might not want to share a varible or
two. This way you have what you have and the other guy
cant f you up.

So now your class has a function that you can call up.
If you want you can paste the code in from the
previous post I sent you which has the same name.

But you need to somehow call that function up during
the windows processing. What do I mean. Well windows
and that includes your dialog based app which is a
windows based app recieves and sends messages to the
windows message que.

All visible objects in a windows application is a
window. Your dialogs static member m_PaintBox is a
handle to a window. It also recieves messages. One of
the messages you can capture is WM_PAINT.

So to call MyPaint() you can call it from a message
that you know comes to your static. The WM_PAINT
message comes when the static box paints itself.

To capture the WM_PAINT message is easy.

Go back to the bottom left of VC and select tab
ClassView. Then select your class MyStatic. "Right
Click" and select Add Window Message Handler. Scroll
down untill you see WM_PAINT. Choose Add and Edit.

Then simply add the line MyPaint(); like so...

void MyStatic::OnPaint()
{
      CPaintDC dc(this);       
      // TODO: Add your message handler code here
      MyPaint();
      
}

Now next thing to do is create a bitmap. Go to the
Resource View and "right click" the top resource
entry.
Select option "insert" and then new and then create
your bitmap. Just draw any simple shape.

Note that the default id is IDB_BITMAP1 that is what I
used in my code to display a bitmap for your example.
Keep the Id the same or change both in the resource
and my MyPaint code to the same thing (otherwize the
bitmap wont show).

Compile and run. Your done.

RJ
 

END EMAIL  /////////////////////////////////////////
RJ
Heres the MyPaint() (api style)

void MyStatic::MyPaint()
{
      HDC dc=::GetDC(this->m_hWnd);
      HDC MemDC=::CreateCompatibleDC(dc);
      RECT rr;::GetClientRect(this->m_hWnd,&rr);
      
      HBITMAP hbmp=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1));
      HBITMAP hOld=(HBITMAP)::SelectObject(MemDC,hbmp);

      BITMAP bmp; ::GetObject(hbmp,sizeof(BITMAP),&bmp);
      
      ::BitBlt(dc,0,0,bmp.bmWidth,bmp.bmHeight,MemDC,0,0,SRCCOPY);


      ::ReleaseDC(this->m_hWnd,dc);
      ::DeleteDC(MemDC);
      ::DeleteObject(hbmp);

}//endfunc