.h
typedef struct ColorImageMes
{
RGBQUAD** BYImageData; //彩色图像像素数据
double** DOImageData;
_In_ int Height; //图像高
_In_ int Width; //图像宽
} ColorimageMes;
class ImageObject : public CObject
{
public:
//打开某路径下的位图并显示在控件中
ColorImageMes OpenEitherFormatImagePictureShow(CStatic *pStaic);
//加载图片
HBITMAP LoadALLPIC(TCHAR * strFileName, int width, int heigh);
//显示图片
void ShowEitherFormatPicture(CString &pathfile, CStatic *pStaic);
//读位图的原图像素数据
ColorImageMes GetImagePixel(CString &Strfile);
};
.cpp
#include "stdafx.h"
#include "ImageClass.h"
#include "Dib.h"
#include "atlbase.h"
#include "atlimage.h"
#include "string"
//
* 功能: 打开某文件夹下的任一格式图片并显示在Picture中
*pStaic:方法:(CStatic*)GetDlgItem(IDC)
**:status:finfish
///
ColorImageMes ImageObject::OpenEitherFormatImagePictureShow(CStatic *pStaic)
{
CString strPath;
CFileDialog dlg(TRUE, NULL, NULL, OFN_NOCHANGEDIR, _T("JPEG/BMP/GIF(*.jpg;*.jpeg;*.bmp;*.gif)|*.jpg;*.jpeg;*.bmp;*.gif|"));
int iReturn = dlg.DoModal();
if (iReturn == IDOK)
{
int nCount = 0;
strPath = dlg.GetPathName();
ShowEitherFormatPicture(strPath,pStaic);
}
return GetImagePixel(strPath);
}
//
//加载某路径下的任意格式图片并显示在控件中
//pathfile:为待显示的文件路径
//PStaic:方法:(CStatic*)GetDlgItem(IDC)
//:status:finfish
//
void ImageObject::ShowEitherFormatPicture(CString &pathfile, CStatic *pStaic)
{
HBITMAP bitmap;
CRect rect; //获得控件的大小
pStaic->GetClientRect(&rect);
int rcWidth = rect.right - rect.left;
int rcHeight = rect.bottom - rect.top;
bitmap = LoadALLPIC(pathfile.GetBuffer(0), rcWidth, rcHeight);
pStaic->ModifyStyle(0xF, SS_BITMAP | SS_CENTERIMAGE);
pStaic->SetBitmap(bitmap);
}
/
加载图片
/
HBITMAP ImageObject::LoadALLPIC(TCHAR * strFileName, int width, int heigh)
{
IPicture* p = NULL;
IStream* s = NULL;
HGLOBAL hG;
void* pp;
FILE* fp;
// Read file in memory
//_wfopen_s(&fp, strFileName, _T("rb"));//Uunicode编码
fp=fopen(strFileName,_T("rb"));//多字节编码
if (!fp)
return NULL;
fseek(fp, 0, SEEK_END);
int fs = ftell(fp);
fseek(fp, 0, SEEK_SET);
hG = GlobalAlloc(GPTR, fs);
if (!hG)
{
fclose(fp);
return NULL;
}
pp = (void*)hG;
fread(pp, 1, fs, fp);
fclose(fp);
CreateStreamOnHGlobal(hG, false, &s);
if (!s)
{
GlobalFree(hG);
return NULL;
}
OleLoadPicture(s, 0, false, IID_IPicture, (void**)&p);
if (!p)
{
GlobalFree(hG);
return NULL;
}
s->Release();
GlobalFree(hG);
HBITMAP hB = 0;
p->get_Handle((unsigned int*)&hB);
// Copy the image.Necessary, because upon p's release,
// the handle is destroyed.
HBITMAP hBB = (HBITMAP)CopyImage(hB, IMAGE_BITMAP, width, heigh, LR_COPYRETURNORG);
p->Release();
return hBB;
}
/
//读原图的数据
*Strfile: 图像所在的路径
*ImageData:返回的图像像素
*WndWidth:返回图像的宽
*WndHeight:返回图像的高
*:status:finfish
/
ColorImageMes ImageObject::GetImagePixel(CString &Strfile)
{
ColorImageMes Picture;
USES_CONVERSION;
WCHAR* pBuf = T2W((LPCTSTR)Strfile); //CString 转 WCHAR
wstring infilename(pBuf);
string outfilename("color.txt");
Bitmap* bmp = new Bitmap(infilename.c_str());
Picture.Height = bmp->GetHeight();
Picture.Width = bmp->GetWidth();
Color color;
Picture.BYImageData = new RGBQUAD*[Picture.Height];
for (int l = 0; l < Picture.Height; l++)
{
Picture.BYImageData[l] = new RGBQUAD[Picture.Width];
}
for (int y = 0; y < Picture.Height; y++)
{
for (int x = 0; x < Picture.Width; x++)
{
bmp->GetPixel(x, y, &color);
Picture.BYImageData[y][x].rgbGreen = (int)color.GetGreen();
Picture.BYImageData[y][x].rgbRed = (int)color.GetRed();
Picture.BYImageData[y][x].rgbBlue = (int)color.GetBlue();
Picture.BYImageData[y][x].rgbReserved = 0;
}
}
delete bmp;
return Picture;
}
基于C++的任意格式图片显示及RGB读取
最新推荐文章于 2024-10-13 14:59:29 发布