用对话框如何实现在图像空间中显示图像
因为之后还要进行图像处理,所以要把图像信息存入内存,然后再显示出来
我新建了一个对话框后,加入了现成的Bitmap.h 和cpp文件
然后在dlg程序中#include "Bitmap.h"后,在打开图像按钮下定义了
MyBitmap *Mybit;
Mybit = new MyBitmap;
这两句之后我应该写什么才能在pic控件中打开并显示图像啊,求代码,各位帮帮我吧
我添加的bitmap.h代码如下:
#if !defined(AFX_BITMAP_H__22369875_B83C_4913_B681_F3CA46404A4C__INCLUDED_)
#define AFX_BITMAP_H__22369875_B83C_4913_B681_F3CA46404A4C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
typedef unsigned char BYTE;
#define PIXEL_OFFSET(i, j, nWidthBytes)\
(LONG)((LONG)(i) * (LONG)(nWidthBytes) + (LONG)(j)*3)
struct ImageDefine{
char ImageTitle[64];
int ImageWidth;
int ImageHeight;
};
class MyBitmap
{
public:
MyBitmap();
~MyBitmap();
public:
int wDIBWidth;
int wDIBHeight;
int wDIBSize;
BYTE *m_pBits;
LPBITMAPFILEHEADER m_lpFileHeader;
LPBITMAPINFOHEADER m_lpInfoHeader;
//open a 24bit bitmap file
void OpenBitmap(char *Filename, ImageDefine id);
//close a bitmap object and release all rsource it used
void CloseBitmap();
//show Bitmap
void ShowBitmap(HDC cmosDC, int startx, int starty);
};
#endif // !defined(AFX_BITMAP_H__22369875_B83C_4913_B681_F3CA46404A4C__INCLUDED_)
我添加的bitmap.cpp的源代码如下
#include "stdafx.h"
#include "Bitmap.h"
#include <time.h>
#include <sys/timeb.h>
#include "stdio.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
MyBitmap::MyBitmap()
{
m_lpFileHeader = NULL;
m_lpInfoHeader = NULL;
m_pBits = NULL;
}
MyBitmap::~MyBitmap()
{
if (m_lpFileHeader != NULL)
delete m_lpFileHeader;
if (m_lpInfoHeader != NULL)
delete m_lpInfoHeader;
if (m_pBits != NULL)
delete m_pBits;
m_lpFileHeader = NULL;
m_lpInfoHeader = NULL;
m_pBits = NULL;
}
void MyBitmap::OpenBitmap(char *Filename, ImageDefine id)
{
wDIBHeight = id.ImageHeight;
wDIBWidth = id.ImageWidth;
if (m_lpFileHeader == NULL)
{
m_lpFileHeader = new BITMAPFILEHEADER;
if (m_lpFileHeader == NULL)
throw "MyBitmap::OpenBitmap--out of memory";
}
if(m_lpInfoHeader == NULL)
{
m_lpInfoHeader = new BITMAPINFOHEADER;
if (m_lpInfoHeader == NULL)
throw "MyBitmap::OpenBitmap--out of memory";
}
if(m_pBits == NULL)
{
m_pBits = new unsigned char[wDIBHeight * wDIBWidth * 3];
}
FILE *fp = fopen(Filename, "rb");
if (!fp)
throw "MyBitmap::OpenBitmap--file open error";
fread(m_lpFileHeader, sizeof(BITMAPFILEHEADER), 1, fp);
fread(m_lpInfoHeader, sizeof(BITMAPINFOHEADER), 1, fp);
wDIBHeight = m_lpInfoHeader->biHeight;
wDIBWidth = m_lpInfoHeader->biWidth;
wDIBSize = wDIBWidth*wDIBHeight*3;
m_lpInfoHeader->biSizeImage = m_lpInfoHeader->biWidth*m_lpInfoHeader->biHeight*3;
fread(m_pBits, 1, m_lpInfoHeader->biSizeImage, fp);
fclose(fp);
}
void MyBitmap::CloseBitmap()
{
delete m_lpFileHeader;
m_lpFileHeader = NULL;
delete m_lpInfoHeader;
m_lpInfoHeader = NULL;
delete []m_pBits;
m_pBits = NULL;
}
void MyBitmap::ShowBitmap(HDC cmosDC, int startx, int starty)
{
HPALETTE m_hPalette;
LOGPALETTE *pLGPal = NULL;
BYTE Palbuf[2048];
int i = 0;
char m_chBmpBuf1[2048];
BITMAPINFO *m_pBmpInfo;
BOOL bSuccess;
pLGPal = (LOGPALETTE *)Palbuf;
pLGPal->palVersion = 0x300;
pLGPal->palNumEntries = 256;
for (i = 0; i < pLGPal->palNumEntries; i++)
{
pLGPal->palPalEntry[i].peRed = (BYTE)i + 10;
pLGPal->palPalEntry[i].peGreen = (BYTE)i + 10;
pLGPal->palPalEntry[i].peBlue = (BYTE)i + 10;
pLGPal->palPalEntry[i].peFlags = 0;//PC_NOCOLLAPSE;
}
m_hPalette = ::CreatePalette(pLGPal);
m_pBmpInfo = (BITMAPINFO *)m_chBmpBuf1;
m_pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_pBmpInfo->bmiHeader.biWidth = wDIBWidth;
m_pBmpInfo->bmiHeader.biHeight = wDIBHeight;
m_pBmpInfo->bmiHeader.biPlanes = 1;
m_pBmpInfo->bmiHeader.biBitCount = 24;
m_pBmpInfo->bmiHeader.biCompression = BI_RGB;
m_pBmpInfo->bmiHeader.biSizeImage = 0;
m_pBmpInfo->bmiHeader.biXPelsPerMeter = 0;
m_pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
m_pBmpInfo->bmiHeader.biClrUsed = 0;
m_pBmpInfo->bmiHeader.biClrImportant = 0;
for (i = 0; i < 256; i++){
m_pBmpInfo->bmiColors[i].rgbBlue = (BYTE)i;
m_pBmpInfo->bmiColors[i].rgbGreen = (BYTE)i;
m_pBmpInfo->bmiColors[i].rgbRed = (BYTE)i;
m_pBmpInfo->bmiColors[i].rgbReserved = 0;
}
/*
strcpy(showstr, "avg_Luma=");
itoa(avg_luma, str,10);
strcat(showstr, str);
TextOut(cmosDC, 20, 50, showstr,12);
*/
bSuccess = StretchDIBits(cmosDC, // hDC
startx, // DestX
starty, // DestY
wDIBWidth, // nDestWidth
wDIBHeight, // nDestHeight
0, // SrcX
0, // SrcY
wDIBWidth, // wSrcWidth
wDIBHeight, // wSrcHeight
m_pBits, // lpBits
m_pBmpInfo, // lpBitsInfo
DIB_RGB_COLORS, // wUsage
SRCCOPY); // dwROP
}