一个是CImage类 一个事CDrawImage类,
第一个是载入图片的,后一个是管理显示的,
可是我用了下面的代码显示一个图片就会出现unhandled exception in .exe,到底该怎么用(⊙o⊙)?
CImage *example=new CImage();
(*example).LoadImage("CGDATA/event.bmp");
CDrawImage *e=new CDrawImage();
e->WipeIn(example,CRect(0,0,640,480),0);下面是这俩个类的代码:
DrawImage。cpp#include "StdAfx.h"
#include "DrawImage.h"
#include "dc.h"
#include "Misc.h"//
// 构造函数
//
CDrawImage::CDrawImage()
:CImage(), hBitmap(0)
{
}//
// 析构函数
//
CDrawImage::~CDrawImage()
{
GdiFlush();
if (hBitmap) {
::DeleteObject(hBitmap);
}
}//
// 制作DIB section
//
BOOL CDrawImage::Create(HDC dc, int width, int height)
{
W = width;
H = height;
D = 24; bytes_per_line = ScanBytes(width, 24);
bytes_per_pixel = PixelBytes(24); InfoHeader.biSize = sizeof(BITMAPINFOHEADER);
InfoHeader.biWidth = width;
InfoHeader.biHeight = height;
InfoHeader.biBitCount = 24;
InfoHeader.biPlanes = 1;
InfoHeader.biXPelsPerMeter = 0;
InfoHeader.biYPelsPerMeter = 0;
InfoHeader.biClrUsed = 0;
InfoHeader.biClrImportant = 0;
InfoHeader.biCompression = BI_RGB;
InfoHeader.biSizeImage = bytes_per_line * height; Info = (BITMAPINFO *)&InfoHeader;
hBitmap = CreateDIBSection(dc, Info, DIB_RGB_COLORS, &Bits, NULL, 0); return hBitmap != 0;
}//
// DIB字符串的描绘
//
void CDrawImage::DrawText(HFONT hFont, int x1, int y1, const char *str, COLORREF color)
{
CMemoryDC dc(0);
HBITMAP oldBitmap = dc.SelectObject(hBitmap);
HFONT oldFont = dc.SelectObject(hFont); dc.SetTextColor(color);
dc.SetBkMode(TRANSPARENT); dc.ExtTextOut(x1, y1, 0, 0, str, strlen(str), NULL); dc.SelectObject(oldBitmap);
dc.SelectObject(oldFont); GdiFlush();
}//
// 擦入处理
//
void CDrawImage::WipeIn(CImage *image, const CRect &rect, int count)
{
int len = rect.Width() * 3;
for (int y=rect.top + count; y<rect.bottom; y+=8) {
memcpy(GetBits(rect.left, y), image->GetBits(rect.left, y), len);
}
}//
// 擦出处理
//
void CDrawImage::WipeOut(const CRect &rect, int count)
{
int len = rect.Width() * 3;
for (int y=rect.top + count; y<rect.bottom; y+=8) {
memset(GetBits(rect.left, y), 0, len);
}
}//
// Title Mixing处理
//
void CDrawImage::Mix(CImage *image, const CRect &rect, int count)
{
static unsigned BitMask[] = {
0x2080, // 0010 0000 1000 0000
0xa0a0, // 1010 0000 1010 0000
0xa1a4, // 1010 0001 1010 0100
0xa5a5, // 1010 0101 1010 0101
0xada7, // 1010 1101 1010 0111
0xafaf, // 1010 1111 1010 1111
0xefbf, // 1110 1111 1011 1111
0xffff, // 1111 1111 1111 1111
} ;
static unsigned XMask[] = {
0xf000, 0x0f00, 0x00f0, 0x000f,
} ;
static unsigned YMask[] = {
0x8888, 0x4444, 0x2222, 0x1111,
} ; for (int y=rect.top; y<rect.bottom; y++) {
unsigned char *p = (unsigned char *)GetBits(rect.left, y);
byte_t *q = (byte_t *)image->GetBits(rect.left, y);
unsigned mask = BitMask[count] & YMask[y & 3]; for (int x=rect.left; x<rect.right; x++) {
if (mask & XMask[x & 3]) {
p[0] = q[0]; // blue
p[1] = q[1]; // green
p[2] = q[2]; // red
}
p += 3;
q += 3;
}
}
}//
// 描绘到CDrawImage的DC类别
//
// 构造函数
//
CImageDC::CImageDC(CDrawImage *image)
:CMemoryDC(0)
{
oldBmp = SelectObject(image->GetBitmapHandle());
}//
// 析构函数
//
CImageDC::~CImageDC()
{
::GdiFlush();
SelectObject(oldBmp);
}DrawImage.h
//
// DIB Section
//
//
#ifndef __DrawImage_h__
#define __DrawImage_h__#include "Image.h"
#include "Misc.h"//
// 使用DIB section的24bit的DIB类别
//
class CDrawImage: public CImage {
public:
CDrawImage();
~CDrawImage(); BOOL Create(HDC dc, int width, int height); void Draw(HDC dc, int x, int y, int w, int h, int ox=0, int oy=0);
void Draw(HDC dc, const CRect &rect, CPoint point);
void Draw(HDC dc, const CRect &rect); void DrawText(HFONT hFont, int x, int y, const char *str, COLORREF color=WhitePixel); void WipeIn(CImage *image, const CRect &rect, int count);
void WipeOut(const CRect &rect, int count);
void Mix(CImage *image, const CRect &rect, int count); HBITMAP GetBitmapHandle() const { return hBitmap; }
protected:
BITMAPINFOHEADER InfoHeader;
HBITMAP hBitmap;
} ;// inline 成员函式// 绘制
//
// 虽然有3种,但除了引数型态不一样之外,动作均同
//
inline void CDrawImage::Draw(HDC dc, int x, int y, int w, int h, int ox, int oy)
{
HDC memdc = CreateCompatibleDC(dc);
HGDIOBJ oldbmp = SelectObject(memdc, hBitmap);
BitBlt(dc, x, y, w, h, memdc, ox, oy, SRCCOPY);
GdiFlush();
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
}
//
// 要描绘到CDrawImage中的 DC 类别
//
class CImageDC: public CMemoryDC {
public:
CImageDC(CDrawImage *image);
~CImageDC(); protected:
HBITMAP oldBmp;
} ;#endif
第一个是载入图片的,后一个是管理显示的,
可是我用了下面的代码显示一个图片就会出现unhandled exception in .exe,到底该怎么用(⊙o⊙)?
CImage *example=new CImage();
(*example).LoadImage("CGDATA/event.bmp");
CDrawImage *e=new CDrawImage();
e->WipeIn(example,CRect(0,0,640,480),0);下面是这俩个类的代码:
DrawImage。cpp#include "StdAfx.h"
#include "DrawImage.h"
#include "dc.h"
#include "Misc.h"//
// 构造函数
//
CDrawImage::CDrawImage()
:CImage(), hBitmap(0)
{
}//
// 析构函数
//
CDrawImage::~CDrawImage()
{
GdiFlush();
if (hBitmap) {
::DeleteObject(hBitmap);
}
}//
// 制作DIB section
//
BOOL CDrawImage::Create(HDC dc, int width, int height)
{
W = width;
H = height;
D = 24; bytes_per_line = ScanBytes(width, 24);
bytes_per_pixel = PixelBytes(24); InfoHeader.biSize = sizeof(BITMAPINFOHEADER);
InfoHeader.biWidth = width;
InfoHeader.biHeight = height;
InfoHeader.biBitCount = 24;
InfoHeader.biPlanes = 1;
InfoHeader.biXPelsPerMeter = 0;
InfoHeader.biYPelsPerMeter = 0;
InfoHeader.biClrUsed = 0;
InfoHeader.biClrImportant = 0;
InfoHeader.biCompression = BI_RGB;
InfoHeader.biSizeImage = bytes_per_line * height; Info = (BITMAPINFO *)&InfoHeader;
hBitmap = CreateDIBSection(dc, Info, DIB_RGB_COLORS, &Bits, NULL, 0); return hBitmap != 0;
}//
// DIB字符串的描绘
//
void CDrawImage::DrawText(HFONT hFont, int x1, int y1, const char *str, COLORREF color)
{
CMemoryDC dc(0);
HBITMAP oldBitmap = dc.SelectObject(hBitmap);
HFONT oldFont = dc.SelectObject(hFont); dc.SetTextColor(color);
dc.SetBkMode(TRANSPARENT); dc.ExtTextOut(x1, y1, 0, 0, str, strlen(str), NULL); dc.SelectObject(oldBitmap);
dc.SelectObject(oldFont); GdiFlush();
}//
// 擦入处理
//
void CDrawImage::WipeIn(CImage *image, const CRect &rect, int count)
{
int len = rect.Width() * 3;
for (int y=rect.top + count; y<rect.bottom; y+=8) {
memcpy(GetBits(rect.left, y), image->GetBits(rect.left, y), len);
}
}//
// 擦出处理
//
void CDrawImage::WipeOut(const CRect &rect, int count)
{
int len = rect.Width() * 3;
for (int y=rect.top + count; y<rect.bottom; y+=8) {
memset(GetBits(rect.left, y), 0, len);
}
}//
// Title Mixing处理
//
void CDrawImage::Mix(CImage *image, const CRect &rect, int count)
{
static unsigned BitMask[] = {
0x2080, // 0010 0000 1000 0000
0xa0a0, // 1010 0000 1010 0000
0xa1a4, // 1010 0001 1010 0100
0xa5a5, // 1010 0101 1010 0101
0xada7, // 1010 1101 1010 0111
0xafaf, // 1010 1111 1010 1111
0xefbf, // 1110 1111 1011 1111
0xffff, // 1111 1111 1111 1111
} ;
static unsigned XMask[] = {
0xf000, 0x0f00, 0x00f0, 0x000f,
} ;
static unsigned YMask[] = {
0x8888, 0x4444, 0x2222, 0x1111,
} ; for (int y=rect.top; y<rect.bottom; y++) {
unsigned char *p = (unsigned char *)GetBits(rect.left, y);
byte_t *q = (byte_t *)image->GetBits(rect.left, y);
unsigned mask = BitMask[count] & YMask[y & 3]; for (int x=rect.left; x<rect.right; x++) {
if (mask & XMask[x & 3]) {
p[0] = q[0]; // blue
p[1] = q[1]; // green
p[2] = q[2]; // red
}
p += 3;
q += 3;
}
}
}//
// 描绘到CDrawImage的DC类别
//
// 构造函数
//
CImageDC::CImageDC(CDrawImage *image)
:CMemoryDC(0)
{
oldBmp = SelectObject(image->GetBitmapHandle());
}//
// 析构函数
//
CImageDC::~CImageDC()
{
::GdiFlush();
SelectObject(oldBmp);
}DrawImage.h
//
// DIB Section
//
//
#ifndef __DrawImage_h__
#define __DrawImage_h__#include "Image.h"
#include "Misc.h"//
// 使用DIB section的24bit的DIB类别
//
class CDrawImage: public CImage {
public:
CDrawImage();
~CDrawImage(); BOOL Create(HDC dc, int width, int height); void Draw(HDC dc, int x, int y, int w, int h, int ox=0, int oy=0);
void Draw(HDC dc, const CRect &rect, CPoint point);
void Draw(HDC dc, const CRect &rect); void DrawText(HFONT hFont, int x, int y, const char *str, COLORREF color=WhitePixel); void WipeIn(CImage *image, const CRect &rect, int count);
void WipeOut(const CRect &rect, int count);
void Mix(CImage *image, const CRect &rect, int count); HBITMAP GetBitmapHandle() const { return hBitmap; }
protected:
BITMAPINFOHEADER InfoHeader;
HBITMAP hBitmap;
} ;// inline 成员函式// 绘制
//
// 虽然有3种,但除了引数型态不一样之外,动作均同
//
inline void CDrawImage::Draw(HDC dc, int x, int y, int w, int h, int ox, int oy)
{
HDC memdc = CreateCompatibleDC(dc);
HGDIOBJ oldbmp = SelectObject(memdc, hBitmap);
BitBlt(dc, x, y, w, h, memdc, ox, oy, SRCCOPY);
GdiFlush();
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
}
//
// 要描绘到CDrawImage中的 DC 类别
//
class CImageDC: public CMemoryDC {
public:
CImageDC(CDrawImage *image);
~CImageDC(); protected:
HBITMAP oldBmp;
} ;#endif
// 24Bits/Pixel 图像
//
//
#include "StdAfx.h"
#include "Application.h"
#include "Image.h"
#include "File.h"
#include "Misc.h"
#include "dc.h"//
// 构造函数
//
CImage::CImage(int width, int height)
{
Create(width, height);
}//
// 产生DIB
//
BOOL CImage::Create(int width, int height)
{
return CDib::Create(width, height, 24);
}//
// 从CGPATH指定的路径载入BMP图档
//
BOOL CImage::LoadImage(const char *name, int ox, int oy)
{
char path[_MAX_PATH];
sprintf(path, CGPATH "%s.bmp", name);
CFile file(path);
if (!file)
return FALSE;
return LoadBMP(file, ox, oy);
}//
// 填满矩形
//
void CImage::FillRect(const CRect &rect, COLORREF color)
{
const unsigned char b = GetBValue(color);
const unsigned char g = GetGValue(color);
const unsigned char r = GetRValue(color); for (int y=rect.top; y<rect.bottom; y++) {
byte_t *p = (byte_t *)GetBits(rect.left, y);
for (int x=rect.left; x<rect.right; x++) {
*p++ = b;
*p++ = g;
*p++ = r;
}
}
}//
// 复制区域
//
void CImage::Copy(const CImage *image, const CRect &rect)
{
int len = rect.Width() * 3;
for (int y=rect.top; y<rect.bottom; y++) {
memcpy(GetBits(rect.left, y), image->GetBits(rect.left, y), len);
}
}//
// 复制区域(有指定复制来源座标)
//
void CImage::Copy(const CImage *image, const CRect &rect, CPoint point)
{
int len = rect.Width() * 3;
for (int y=rect.top; y<rect.bottom; y++) {
memcpy(GetBits(rect.left, y), image->GetBits(point.x, point.y++), len);
}
}//
// 复制(考虑了透明颜色)
//
void CImage::MixImage(const CImage *image, const CRect &rect, COLORREF trans_color)
{
const unsigned char trans_b = GetBValue(trans_color);
const unsigned char trans_g = GetGValue(trans_color);
const unsigned char trans_r = GetRValue(trans_color); for (int y=rect.top; y<rect.bottom; y++) {
byte_t *p = (byte_t *)GetBits(rect.left, y);
const byte_t *q = (byte_t *)image->GetBits(rect.left, y);
for (int x=rect.left; x<rect.right; x++) {
const byte_t b = *q++;
const byte_t g = *q++;
const byte_t r = *q++; if (b != trans_b || g != trans_g || r != trans_r) {
p[0] = b;
p[1] = g;
p[2] = r;
}
p += 3;
}
}
}//
// 复制(考虑了透明颜色)
//
void CImage::MixImage(const CImage *image, const CRect &rect, CPoint point, COLORREF trans_color)
{
const unsigned char trans_b = GetBValue(trans_color);
const unsigned char trans_g = GetGValue(trans_color);
const unsigned char trans_r = GetRValue(trans_color); for (int y=rect.top; y<rect.bottom; y++) {
byte_t *p = (byte_t *)GetBits(rect.left, y);
const byte_t *q = (byte_t *)image->GetBits(point.x, point.y++);
for (int x=rect.left; x<rect.right; x++) {
const byte_t b = *q++;
const byte_t g = *q++;
const byte_t r = *q++; if (b != trans_b || g != trans_g || r != trans_r) {
p[0] = b;
p[1] = g;
p[2] = r;
}
p += 3;
}
}
}//
// 绘制矩形
//
void CImage::DrawRect(const CRect &rect, COLORREF color)
{
int width = rect.Width();
int height = rect.Height();
FillRect(rect.left, rect.top, width, 1, color);
FillRect(rect.left, rect.top, 1, height, color);
FillRect(rect.right - 1, rect.top, 1, height, color);
FillRect(rect.left, rect.bottom - 1, width, 1, color);
}//
// 填入透明度50%的黑色
//
void CImage::FillHalfToneRect(const CRect &rect)
{
for (int y=rect.top; y<rect.bottom; y++) {
byte_t *p = (byte_t *)GetBits(rect.left, y);
for (int x=rect.left; x<rect.right; x++) {
*p++ /= 2;
*p++ /= 2;
*p++ /= 2;
}
}
}//
// 绘制边框
//
void CImage::DrawFrameRect(int x, int y, int w, int h, COLORREF color)
{
DrawRect(x, y + 1, w, h - 2, color);
DrawRect(x + 1, y, w - 2, h, color);
FillHalfToneRect(x + 2, y + 2, w - 4, h - 4);
}CImage.h//
// 24Bits/Pixel图像
//
//
#ifndef __image_h
#define __image_h#include "Dib.h"
#include "Misc.h"class CDC;//
// 专给24bit使用的CIB类别
//
class CImage: public CDib {
public:
CImage(): CDib() {}
CImage(int width, int height); BOOL Create(int width, int height); BOOL LoadImage(const char *name, int ox=0, int oy=0); void Copy(const CImage *image, const CRect &rect, CPoint point);
void Copy(const CImage *image, const CRect &rect);
void Copy(const CImage *image);
void FillRect(const CRect &rect, COLORREF color);
void FillRect(int x, int y, int w, int h, COLORREF color);
void MixImage(const CImage *image, const CRect &rect, COLORREF trans=RGB(0, 255, 0));
void MixImage(const CImage *image, const CRect &rect, CPoint src_pos, COLORREF trans=RGB(0, 255, 0));
void DrawRect(const CRect &rect, COLORREF color);
void DrawRect(int x, int y, int w, int h, COLORREF color);
void FillHalfToneRect(const CRect &rect);
void FillHalfToneRect(int x, int y, int w, int h);
void DrawFrameRect(const CRect &rect, COLORREF color=WhitePixel);
void DrawFrameRect(int x, int y, int w, int h, COLORREF color=WhitePixel);
COLORREF GetPixel(CPoint point) const;
} ;// inline 成员函式inline void CImage::Copy(const CImage *image)
{
Copy(image, CRect(0, 0, image->Width(), image->Height()));
}inline void CImage::FillRect(int x, int y, int w, int h, COLORREF color)
{
FillRect(CRect(x, y, x + w, y + h), color);
}inline void CImage::DrawRect(int x, int y, int w, int h, COLORREF color)
{
DrawRect(CRect(x, y, x + w, y + h), color);
}inline void CImage::FillHalfToneRect(int x, int y, int w, int h)
{
FillHalfToneRect(CRect(x, y, x + w, y + h));
}inline void CImage::DrawFrameRect(const CRect &rect, COLORREF color)
{
DrawFrameRect(rect.left, rect.top, rect.Width(), rect.Height(), color);
}inline COLORREF CImage::GetPixel(CPoint point) const
{
const byte_t *p = (byte_t *)GetBits(point.x, point.y);
return RGB(p[2], p[1], p[0]);
}#endif分数可以多追加(但是为防止没人回答先给的分数就少点,呵呵),请大家帮我解决一下吧,谢谢了!
CImage *example=new CImage();
(*example).LoadImage("CGDATA/event.bmp");
CDrawImage *e=new CDrawImage();
e->WipeIn(example,CRect(0,0,640,480),0);
用来将载入的位图显示出来,但是运行的时候提示错误,貌似是指针出错了,可我实在是找不出来呢。
还有就是关于CDrawImage::Draw()函数,这个绘制函数本应该用到的,不过还是不知道该怎么用,大家帮帮忙吧,O(∩_∩)O谢谢
就可以在CImage对象Load完成后,在OnDraw或者OnPaint里用dc贴出来。:)