Jump to content
  • Advertisement
Sign in to follow this  
Bobalandi

C++ BitBlt Draw Bitmap

This topic is 3847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I've loaded a bitmap to my app with the code below, but I am trying to use bitblt to mask the black out of it, and then draw it to a specific place on the screen, does anyone have any ideas how to do it? Thanks, help is appreciated.
//BitMapObject.h
#ifndef BITMAPOBJECT_H
#define BITMAPOBJECT_H
#pragma once
#include <windows.h>

class BitMapObject
{
private:
	HDC hdcMemory;
	HBITMAP hbmNewBitMap;
	HBITMAP hbmOldBitMap;
	int iWidth;
	int iHeight;
public:
	//constructor
	BitMapObject();
	//destructor
	~BitMapObject();

	void Load(HDC hdcCompatible,LPCTSTR lpszFilename);
	void Create(HDC hdcCompatible, int width, int height);
	//destroys bitmap and dc
	void Destroy();
	int GetWidth();
	int GetHeight();
	//converts to HDC
	operator HDC();
};

#endif

//Bitmapobject.cpp
#include "bitmapobject.h"

BitMapObject::BitMapObject()
{
	hdcMemory=NULL;
	hbmNewBitMap=NULL;
	hbmOldBitMap=NULL;
	iWidth=0;
	iHeight=0;
}

BitMapObject::~BitMapObject()
{
	//if the hdcMemory hasn't been destroyed, do so
	if(hdcMemory)
		Destroy();
}

void BitMapObject::Load(HDC hdcCompatible, LPCTSTR lpszFilename)
{
	//if hdcMemory isn't null, make it so captain!
	if(hdcMemory)
		Destroy();
	//create memory dc.
	hdcMemory=CreateCompatibleDC(hdcCompatible);
	//load the bitmap
	hbmNewBitMap=(HBITMAP)LoadImage(NULL,lpszFilename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	//shove the image into the dc
	hbmOldBitMap=(HBITMAP)SelectObject(hdcMemory,hbmNewBitMap);
	//grab the bitmap's properties
	BITMAP bmp;
	GetObject(hbmNewBitMap,sizeof(BITMAP),(LPVOID)&bmp);
	iWidth=bmp.bmWidth;
	iHeight=bmp.bmHeight;
}

void BitMapObject::Create(HDC hdcCompatible, int width, int height)
{
	//if hdcMemory isn't null, blow it up!
	if(hdcMemory)
		Destroy();

	//create the memory dc.
	hdcMemory=CreateCompatibleDC(hdcCompatible);
	//create the bitmap
	hbmNewBitMap=CreateCompatibleBitmap(hdcCompatible, width, height);
	//shove the image into the dc
	hbmOldBitMap=(HBITMAP)SelectObject(hdcMemory, hbmNewBitMap);
	iWidth=width;
	iHeight=height;
}

void BitMapObject::Destroy()
{
	//restore old bitmap.
	SelectObject(hdcMemory, hbmOldBitMap);
	//delete new bitmap.
	DeleteObject(hbmNewBitMap);
	//delete device context.
	DeleteDC(hdcMemory);
	//set members to 0/NULL
	hdcMemory=NULL;
	hbmNewBitMap=NULL;
	hbmOldBitMap=NULL;
	iWidth=0;
	iHeight=0;
}

BitMapObject::operator HDC()
{
	return(hdcMemory);
}

int BitMapObject::GetWidth()
{
	return(iWidth);
}

int BitMapObject::GetHeight()
{
	return(iHeight);
}

Share this post


Link to post
Share on other sites
Advertisement



hBackA = (HBITMAP) LoadImage (hInst, "coat.bmp", IMAGE_BITMAP, 0, 0,LR_LOADFROMFILE);
hdcBackA = CreateCompatibleDC (NULL) ;
SelectObject(hdcBackA, hBackA);

hdcBack = CreateCompatibleDC(NULL);
hdcObject = CreateCompatibleDC(NULL);
hdcMem = CreateCompatibleDC(NULL);
hdcSave = CreateCompatibleDC(NULL);

bmAndBack = CreateBitmap(300, 300,1,1,NULL); //MONOCHROME BITMAPs!!
bmAndObject = CreateBitmap(300, 300,1,1,NULL);
bmAndMem = CreateCompatibleBitmap(hdc, 300, 300);
hSave = CreateCompatibleBitmap(hdc, 300, 300);

SelectObject(hdcBack, bmAndBack);
SelectObject(hdcObject, bmAndObject);
SelectObject(hdcMem, bmAndMem);
SelectObject(hdcSave, hSave);





BOOL cGraphics::GDI_Blit(int bx, int by, int bw, int bh, int lx, int ly, BOOL Transparent)
{
//hWnd=GethWnd();
//BitBlt (hdc, bx, by, bw, bh, hdcBackA, lx, ly, SRCCOPY) ;

if (Transparent==Trans)
{
COLORREF cColor;
BitBlt(hdcSave, 0, 0, 300, 300, hdcTemp, 0, 0, SRCCOPY); // Backup hTemp into hSave ERROR, should be whole bitmap

cColor = SetBkColor(hdcTemp, RGB(0,0,0)); // SET TRANSPARENT COLOR !!
BitBlt(hdcObject, 0, 0, bw, bh, hdcTemp, lx, ly, SRCCOPY);
SetBkColor(hdcTemp, cColor);

BitBlt(hdcBack, 0, 0, bw, bh, hdcObject, 0, 0, NOTSRCCOPY);
BitBlt (hdcMem, 0, 0, bw, bh, hdcFlip, bx, by, SRCCOPY) ;
BitBlt(hdcMem, 0, 0, bw, bh, hdcObject, 0, 0, SRCAND);

BitBlt(hdcTemp, lx, ly, bw, bh, hdcBack, 0, 0, SRCAND);
BitBlt(hdcMem, 0, 0, bw, bh, hdcTemp, lx, ly, SRCPAINT);
BitBlt(hdcFlip, bx, by, bw, bh, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcTemp, 0, 0, bw, bh, hdcSave, 0, 0, SRCCOPY); // Move back save to temp
} // End if (Transparent==Trans)
else
BitBlt(hdcFlip, bx, by, bw, bh, hdcTemp, lx, ly, SRCCOPY);

return TRUE;
} // End GDI_Blit

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!