鱼C论坛

 找回密码
 立即注册

WIN32程序的IMAGE类!

已有 541 次阅读2011-9-14 13:04

//头文件Image.h ­

#pragma once ­

#include <windows.h> ­

#include <wingdi.h> ­

class Image                         //图片类 ­

{ ­

protected: ­

HDC   m_hDC; ­

HDC   m_hMaskDC; ­

HDC   m_hTempDC; ­

HBITMAP  m_hBitmap; ­

HBITMAP  m_hBitmapMask; ­

HBITMAP  m_hBitmapTemp; ­

int   m_nWidth; ­

int   m_nHeight; ­

public: ­

Image(void); ­

~Image(void); ­

­

bool LoadImage( char* szFileName ); ­

void DrawImage( HDC& hDC, int nX, int nY ); ­

void    DrawImage( HDC& hDC, int nX, int nY , COLORREF color); ­

void DrawImage( HDC& hDC, int nX, int nY, int nRectX, int nRectY, int nRectW, int nRectH );                  //扣背景色 ­

void DrawImage( HDC& hDC, int nX, int nY, int nRectX, int nRectY, int nRectW, int nRectH ,COLORREF color);   //扣背景色 ­

void    DrawImageTran( HDC& hDC, int nX, int nY, int nRectX, int nRectY, int nRectW, int nRectH ,COLORREF color);//去背景色并且翻转 ­

void Translucence( HDC &hBufferDC, int nViewX, int nViewY, int nPlayX, int nPlayY, double fAlpha ); ­

void LoadTemp(HDC& hDC); ­

void DrawImageTemp( HDC& hDC,HDC& m_hDC, int nX, int nY ); ­

int  GetWidth() { return m_nWidth; } ­

int  GetHeight() { return m_nHeight; } ­

}; ­

­

­

­

//源文件 Image.cpp­

#include "Image.h" ­

­

Image::Image(void) ­

{ ­

m_hDC = NULL; ­

m_hMaskDC = NULL; ­

m_hTempDC = NULL; ­

m_hBitmap = NULL; ­

m_hBitmapMask = NULL; ­

m_hBitmapTemp = NULL; ­

m_nWidth = 0; ­

m_nHeight = 0; ­

} ­

Image::~Image(void) ­

{ ­

if( m_hBitmap ) ­

{ ­

  m_hBitmap = (HBITMAP)SelectObject( m_hDC, m_hBitmap ); ­

  DeleteObject( m_hBitmap ); ­

  m_hBitmap = NULL; ­

} ­

if( m_hDC ) DeleteDC( m_hDC ); ­

if( m_hBitmapMask ) ­

{ ­

  m_hBitmapMask = (HBITMAP)SelectObject( m_hMaskDC, m_hBitmapMask ); ­

  DeleteObject( m_hBitmapMask ); ­

  m_hBitmapMask = NULL; ­

} ­

if( m_hMaskDC ) DeleteDC( m_hMaskDC ); ­

if( m_hBitmapTemp ) ­

{ ­

  m_hBitmapTemp = (HBITMAP)SelectObject( m_hTempDC, m_hBitmapTemp ); ­

  DeleteObject( m_hBitmapTemp ); ­

  m_hBitmapTemp = NULL; ­

} ­

if( m_hTempDC ) DeleteDC( m_hTempDC ); ­

} ­

bool Image::LoadImage( char* szFileName ) ­

{ ­

if( m_hBitmap ) ­

{ ­

  m_hBitmap = (HBITMAP)SelectObject( m_hDC, m_hBitmap ); ­

  DeleteObject( m_hBitmap ); ­

  m_hBitmap = NULL; ­

} ­

if( m_hDC ) DeleteDC( m_hDC ); ­

m_hBitmap = (HBITMAP)::LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE ); ­

if( !m_hBitmap ) return false; ­

BITMAP bmp; ­

GetObject( m_hBitmap, sizeof(BITMAP), &bmp ); ­

m_nWidth = bmp.bmWidth;  //图片宽 ­

m_nHeight = bmp.bmHeight; //图片高 ­

m_hDC = CreateCompatibleDC( NULL ); ­

m_hTempDC=CreateCompatibleDC( NULL ); ­

m_hBitmap = (HBITMAP)SelectObject( m_hDC, m_hBitmap ); ­

m_hBitmapTemp=CreateCompatibleBitmap(m_hDC,m_nWidth,m_nHeight); ­

SelectObject(m_hTempDC,m_hBitmapTemp); ­

return true; ­

} ­

­

­

void Image::DrawImage( HDC& hDC, int nX, int nY ) ­

{ ­

BitBlt( hDC, nX, nY, m_nWidth, m_nHeight, m_hDC, 0, 0, SRCCOPY ); ­

} ­

void Image::DrawImage( HDC& hDC, int nX, int nY, int nRectX, int nRectY, int nRectW, int nRectH ) ­

{ ­

BitBlt( hDC, nX, nY, nRectW, nRectH, m_hDC, nRectX, nRectY, SRCCOPY ); ­

} ­

//半透明处理-------------------------------------------------------------------------------------------------------------- ­

void Image::Translucence( HDC &hBufferDC, int nViewX, int nViewY, int nPlayX, int nPlayY, double fAlpha ) ­

{ ­

struct  ColorRGB ­

{ ­

  int  byRed; ­

  int  byGreen; ­

  int  byBlue; ­

}; ­

COLORREF nC1; //源点颜色 ­

COLORREF nC2; //目标点颜色 ­

HDC   hDC; //半透明缓冲区DC ­

HBITMAP  m_hBitmap; ­

hDC = CreateCompatibleDC( m_hDC ); ­

m_hBitmap = (HBITMAP)SelectObject( hDC, CreateCompatibleBitmap( m_hDC, m_nWidth, m_nHeight ) ); ­

ColorRGB sRGB[3]; ­

for ( int ni = 0; ni < m_nHeight; ni++ ) ­

  for ( int nj = 0; nj < m_nWidth; nj++ ) ­

  { ­

   //背景图 ­

   nC1 = GetPixel( hBufferDC, nPlayX - nViewX + nj, nPlayY - nViewY + ni ); ­

   sRGB[0].byRed = GetRValue( nC1 ); ­

   sRGB[0].byGreen = GetGValue( nC1 ); ­

   sRGB[0].byBlue = GetBValue( nC1 ); ­

   //目标图 ­

   nC2 = GetPixel( m_hDC, nj, ni ); ­

   sRGB[1].byRed = GetRValue( nC2 ); ­

   sRGB[1].byGreen = GetGValue( nC2 ); ­

   sRGB[1].byBlue = GetBValue( nC2 ); ­

   if ( sRGB[1].byRed == 255 && sRGB[1].byGreen == 0 && sRGB[1].byBlue == 255 ) ­

   { ­

    SetPixel( hDC, nj, ni, nC1 ); ­

   } ­

   else ­

   { ­

    //半透明处理 ­

    sRGB[2].byRed = (int)( sRGB[0].byRed * fAlpha + sRGB[1].byRed * ( 1.0001 - fAlpha ) ); ­

    sRGB[2].byGreen = (int)( sRGB[0].byGreen * fAlpha + sRGB[1].byGreen * ( 1.0001 - fAlpha ) ); ­

    sRGB[2].byBlue = (int)( sRGB[0].byBlue * fAlpha + sRGB[1].byBlue * ( 1.0001 - fAlpha ) ); ­

    SetPixel( hDC, nj, ni, RGB( sRGB[2].byRed, sRGB[2].byGreen, sRGB[2].byBlue ) ); ­

   } ­

  } ­

  BitBlt( hBufferDC, nPlayX - nViewX, nPlayY - nViewY, m_nWidth, m_nHeight, hDC, 0, 0, SRCCOPY ); ­

} ­

void Image::DrawImage(HDC& hDC, int nX, int nY , COLORREF color) ­

{ ­

TransparentBlt(hDC,nX,nY,m_nWidth,m_nHeight,m_hDC,0,0,m_nWidth,m_nHeight,color); ­

} ­

void Image::DrawImage( HDC& hDC, int nX, int nY, int nRectX, int nRectY, int nRectW, int nRectH ,COLORREF color ) ­

{ ­

TransparentBlt(hDC,nX,nY,nRectW,nRectH,m_hDC,nRectX,nRectY,nRectW,nRectH,color); ­

} ­

void Image::DrawImageTran(HDC& hDC, int nX, int nY, int nRectX, int nRectY, int nRectW, int nRectH ,COLORREF color) ­

{ ­

StretchBlt(m_hTempDC, 0, 0, nRectW, nRectH, m_hDC, nRectX+nRectW, nRectY, nRectW*(-1), nRectH, SRCCOPY); ­

TransparentBlt(hDC,nX,nY,nRectW,nRectH,m_hTempDC,0,0,nRectW,nRectH,color); ­

} ­

void Image::DrawImageTemp( HDC& hDC,HDC& m_hDC, int nX, int nY ) ­

{ ­

BitBlt( hDC, nX, nY, 1000, 1000, m_hDC, 0, 0, SRCCOPY ); ­

} ­


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-13 00:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部