How i can use a bitmap as background image?

  • Thread starter TenNen
  • Start date
  • Tags
    Image
In summary: By setting a 'background' your painting a DIB bitmap (if I recall, its been about two years since I've done this because I use a Mac, and Linux) at the lowest level and then placing all of your objects on top of that picture (buttons, other BMP's and the like). TenNen here's a code snip from an old program written for win98. I wrote it in C so it might not work on XP (if that's what you're using). You might want ot buy a book or goto a library and get an MFC book.Anyway the following is the WndPro
  • #1
TenNen
97
0
how i can use a bitmap as background image ?
Thanks
 
Computer science news on Phys.org
  • #2
In what? Are you using Gnome? Are you using KDE? Are you using the WinAPI? Are you using Mac OS X? Are you using another windowing environment? Are you trying to set the background for a particular piece of software (AIM for example)?

If you're trying to do this in a piece of software of your own design which platform are you running? Are you using an IDE if so which?

Most IDE's have the ability to select a background bmp and generate the appropriate code for you. I know KDevelope does, Glade(2) Does, Mac Interface Builder does, and I'm almost (+) VC++ does.

[edit]The Qt interface builder does, not KDevelope. Qt interface builder can be invoke from KDevelope though before any KDE users correct me.
 
Last edited:
  • #3
faust9 said:
In what? Are you using Gnome? Are you using KDE? Are you using the WinAPI? Are you using Mac OS X? Are you using another windowing environment? Are you trying to set the background for a particular piece of software (AIM for example)?

If you're trying to do this in a piece of software of your own design which platform are you running? Are you using an IDE if so which?

Most IDE's have the ability to select a background bmp and generate the appropriate code for you. I know KDevelope does, Glade(2) Does, Mac Interface Builder does, and I'm almost (+) VC++ does.

[edit]The Qt interface builder does, not KDevelope. Qt interface builder can be invoke from KDevelope though before any KDE users correct me.
faust9,
I am sorry about the author problem, Okay ?
I did never mean to correct you or anything...True!
I am using winApi, I just do simple things, I don't know how to use a bitmap as a backgound image instead of just setting bkgound to numbers (0,1,2,3,4,5,18), can you help me ?

Help me please..
 
  • #4
What are you trying to set the background of? I am racking my brain trying to think of something in which the background is a number but can't. Start over from the beginning and tell us exactly what you are trying to set the background of, on what platform, in what programming language and then hopfully we will be able to help.

I think I got that this was windows. Is this a GUI application? Console?

Is this C++/MFC, .NET, web programming, or something totally different?
 
  • #5
Anteros said:
What are you trying to set the background of? I am racking my brain trying to think of something in which the background is a number but can't. Start over from the beginning and tell us exactly what you are trying to set the background of, on what platform, in what programming language and then hopfully we will be able to help.

I think I got that this was windows. Is this a GUI application? Console?

Is this C++/MFC, .NET, web programming, or something totally different?


Background isn't a easly selectible function parameter, its a little convaluted actually. By setting a 'background' your painting a DIB bitmap (if I recall, its been about two years since I've done this because I use a Mac, and Linux) at the lowest level and then placing all of your objects on top of that picture (buttons, other BMP's and the like).

TenNen here's a code snip from an old program written for win98. I wrote it in C so it might not work on XP (if that's what you're using). You might want ot buy a book or goto a library and get an MFC book.

Anyway the following is the WndProc function I used:

Code:
LRESULT CALLBACK WndProc	(HWND hWnd,
				 UINT ui<essage,
				 WPARAM wParam,
				 LPARAM lParam)
  {
    static BITMAPFILEHEADER *pbmfh;
    static BITMAPINFO *pbmi;
    static BYTE *pBits;
    static int cxDib, cyDib;

    DWORD dwFileSize, dwHighSize, dwBytesRead;
    HANDLE hFile;
    hFile = CreateFile	("C:\\!place file path here, lose the ! keep the quotes",
			 GENERIC_READ,
			 FILE_SHARE_READ,
			 NULL,
			 OPEN_EXISTING,
			 FILE_FLAG_SEQUENTIAL_SCAN,
			 NULL);
    dwFileSize = GetFileSize(hFile, &bdHighSize);
    pbmfh = (BITMAPHEADER) * malloc (dxFileSize);
    ReadFile	(hFile,
		 pbmfh,
		 dwFileSize,
		 &dwBytesRead,
		 NULL);
    pbmi= (BITMAPINFO) * (pbmfh+1);
    pBits = (BYTE) * pbmfh + pbmfh->bfOffBits;
    cxDib = pbmi->bmiHeader.biWidth;
    cyDib = abs(pbmi->bmiHeader.biHeight);
    HDC hdc;
    hdc = GetDC(hWnd);
    SetDIBitsToDevice	(hdc,
			 0,
			 0,
			 cxDib,
			 cyDib,
			 0,
			 0,
			 0,
			 cyDib,
			 pBits,
			 pbmi,
			 DIB_RGB_COLOR);
    ReleaseDC (hWind, hdc);
    free (pbmfh);
    return 0;
}

Again, the above may or may not work. I don't know if the program I cut it from worked or not because I haven't used MS in so long. It's not a simple matter of calling a function with the file location, you have to jump through some hoops.

Well, hope this helped. Good luck.
 
  • #6
Ok, so if in fact we are talking about MFC, there is a pretty useful class which I use from time to time over on Codeproject http://www.codeproject.com/bitmap/picturewindow.asp . It extends an ATL window class so you have to insert a couple lines (most likely in your stdafx.h)

#include < atlbase.h > ;
extern CComModule _Module;

You then make whatever object you want, a subclass of the included header, call load("somepicture") and you're good to go. The cool part is, the pictureWindow class gets a nice pointer to an IPicture interface and gets a nice Render() method so you don't have to deal with DIB directly.

And here's the class. Visit the Codeproject link for more info. It's a really nice, small, reusable class. You do get some overhead with having to bring in atlbase.h but it might be worth it for you.

Enjoy.


Code:
/************************************
  REVISION LOG ENTRY
  Revision By: Mihai Filimon 
  Revised on 11/24/99 9:45:45 AM
  Comments: PictureWindow.h: interface for the CPictureWindow class.
 ************************************/

#if !defined(AFX_PICTUREWINDOW_H__44323373_9E89_11D3_A393_00C0DFC59237__INCLUDED_)
#define AFX_PICTUREWINDOW_H__44323373_9E89_11D3_A393_00C0DFC59237__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <atlbase.h>
#include <atlwin.h>

class CPictureWindow  : public CWindowImpl<CPictureWindow>
{
public:
	typedef enum HandlerTypeEnum
	{
		ClientPaint = WM_PAINT,
		BackGroundPaint = WM_ERASEBKGND
	} HandlerTypeEnum;

	CPictureWindow()
	{
		m_nMessageHandler = ClientPaint;
	};
	virtual ~CPictureWindow()
	{
	};

	BEGIN_MSG_MAP(CPictureWindow)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkGnd)
	END_MSG_MAP()

	// Function name	: Load
	// Description	    : Loads the picture into memory, and then display it!
	// Return type		: virtual BOOL 
	// Argument         : LPCTSTR szFileName
	virtual BOOL Load( LPCTSTR szFileName )
	{
		BOOL bResult = FALSE;
		Close();
		if ( szFileName )
		{
			OFSTRUCT of;
			HANDLE hFile = NULL;;
			if ( (hFile = (HANDLE)OpenFile( szFileName, &of, OF_READ | OF_SHARE_COMPAT)) != (HANDLE)HFILE_ERROR )
			{
				DWORD dwHighWord = NULL, dwSizeLow = GetFileSize( hFile, &dwHighWord );
				DWORD dwFileSize = dwSizeLow;
				HRESULT hResult = NULL;
				if ( HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize) )
					if ( void* pvData = GlobalLock( hGlobal ) )
					{
						DWORD dwReadBytes = NULL;
						BOOL bRead = ReadFile( hFile, pvData, dwFileSize, &dwReadBytes, NULL );
						GlobalUnlock( hGlobal );
						if ( bRead )
						{
							CComPtr<IStream> spStream;
							_ASSERTE( dwFileSize == dwReadBytes );
							if ( SUCCEEDED( CreateStreamOnHGlobal( hGlobal, TRUE, &spStream) ) )
								if ( SUCCEEDED( hResult = OleLoadPicture( spStream, 0, FALSE, IID_IPicture, (void**)&m_spPicture ) ) )
									bResult = TRUE;
						}
					}
				CloseHandle( hFile );
			}
		}
		Invalidate();
		return bResult;
	}

	HandlerTypeEnum m_nMessageHandler;

protected:

	inline virtual BOOL IsHandlerMessage( UINT uMsg )
	{
		return m_nMessageHandler == (HandlerTypeEnum)uMsg ;
	}

	void PutPicture( IPicture* pPicture, HDC hDC, RECT rPicture )
	{
		OLE_XSIZE_HIMETRIC nWidth = NULL; OLE_YSIZE_HIMETRIC nHeight = NULL;
		pPicture->get_Width( &nWidth ); pPicture->get_Height( &nHeight );
		pPicture->Render( hDC, rPicture.left ,rPicture.top, rPicture.right - rPicture.left, rPicture.bottom - rPicture.top, 0, nHeight, nWidth, -nHeight, NULL );
	};

	LRESULT OnEraseBkGnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		if ( IsHandlerMessage( uMsg ) )
		{
			if ( m_spPicture )
			{
				BeginPaint( NULL );
					RECT r ; GetClientRect( &r );
					HDC hDC = GetDC();
					HWND hWndChild = GetWindow( GW_CHILD );
					while ( ::IsWindow( hWndChild ) )
					{
						if ( ::IsWindowVisible( hWndChild ) )
						{
							RECT rChild; ::GetWindowRect( hWndChild, &rChild );
							ScreenToClient( &rChild );
							ExcludeClipRect( hDC, rChild.left, rChild.top, rChild.right, rChild.bottom );
						}
						hWndChild = ::GetWindow( hWndChild, GW_HWNDNEXT );
					}
					PutPicture( m_spPicture, hDC, r );
					ReleaseDC( hDC );
				EndPaint( NULL );
				return TRUE;
			}
		}
		bHandled = FALSE;
		return FALSE;
	};

	LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		if ( IsHandlerMessage( uMsg ) )
		{
			if ( m_spPicture )
			{
				BeginPaint( NULL );
				RECT r ; GetClientRect( &r );
				HDC hDC = GetDC();
				PutPicture( m_spPicture, hDC, r );
				ReleaseDC( hDC );
				EndPaint( NULL );
			}
		}
		bHandled = FALSE;
		return NULL;
	};

	void Close()
	{
		m_spPicture = NULL;
	}

	//Attributes
	CComPtr<IPicture> m_spPicture;
};

#endif // !defined(AFX_PICTUREWINDOW_H__44323373_9E89_11D3_A393_00C0DFC59237__INCLUDED_)
 
Last edited by a moderator:
  • #7
Thank you so very much, you two are really kewl...
I can't be online now, I am really busy, we are having a break but I will get back a few hours later to post the results of whether or not my program works fine.

Again, thanks a lot.
 
  • #8
Anteros, I am coding to make a Win32 application from scratch, I amnot using MFC... I am still new to API, I have known about it for long but never tried to learn it,
I am actually trying to draw a clock, I think if I can use something as a background image, my application would look better, but I am stuck at it...

faust9,
Yes, I couldn't make your code work on my system.
Thanks a lot anyway,
 
  • #9
TenNen said:
Anteros, I am coding to make a Win32 application from scratch, I amnot using MFC... I am still new to API, I have known about it for long but never tried to learn it,
I am actually trying to draw a clock, I think if I can use something as a background image, my application would look better, but I am stuck at it...

That's fine, this class doesn't have to be used from MFC. In fact, it's an ATL class so it's not native MFC at all. Just create an instance of the PictureWindow class and call the SubclassWindow() method on this class giving it a HWND. This can be a handle to a dialog, control, whatever. Then call the Load() method and you're good to go.

If you don't want to use the ATL libraries at all, well then you're on your own. Check http://www.codeproject.com for some more info. I think I saw a few articles there that may apply. I would say give that class a shot though and see if it works for you.
 
  • #10
Okay, thanks a lot,
 

1. How do I insert a bitmap as a background image on a webpage?

To insert a bitmap as a background image on a webpage, you can use the CSS background-image property. Here's an example of the code:
body { background-image: url("image.jpg");}

2. Can I use any type of bitmap as a background image?

Yes, you can use any type of bitmap as a background image as long as it is supported by web browsers. Some common bitmap formats include JPEG, PNG, and GIF.

3. How do I make the background image cover the entire webpage?

To make the background image cover the entire webpage, you can use the CSS background-size property. Set the value to "cover" and it will automatically resize the image to cover the entire webpage.

4. Can I use multiple bitmaps as background images?

Yes, you can use multiple bitmaps as background images by using the CSS multiple background image syntax. Here's an example:
body { background-image: url("image1.jpg"), url("image2.jpg");}

5. How do I position the background image on a specific part of the webpage?

You can use the CSS background-position property to position the background image on a specific part of the webpage. You can specify the position using keywords (e.g. top, bottom, left, right) or using pixel values.

Similar threads

  • Computing and Technology
Replies
11
Views
3K
Replies
31
Views
2K
Replies
7
Views
669
Replies
73
Views
5K
Replies
5
Views
1K
  • Computing and Technology
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
976
Replies
5
Views
1K
  • Computing and Technology
Replies
7
Views
1K
  • Computing and Technology
Replies
1
Views
232
Back
Top