mfc 灰度图像显示及放大缩小功能的实现
template <class T1, class T2>
void CchinaDiankeSarDisPlayImagDlg::DisplayGrayImag(T1* pgrayData, int srcWidth, int srcHeight, int extractNum, float coefficient, int dstWidth, int dstHeight)
{
try
{
//ModifyPictureCtrlSize();
COLORREF *data;
CBitmap bm;
CDC *MemDC = new CDC;
int width = dstWidth, height = dstHeight;
CDC* pDC = GetDlgItem(IDC_STATIC)->GetDC();// 获取 HDC(设备句柄) 来进行绘图操作
pDC->SetBkMode(TRANSPARENT); //设置背景颜色为透明
data = new COLORREF[width * height];//为data数据分配空间,大小为灰度图像的大小
//为data赋值
int itmp = 0;
T2 val;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
val = pgrayData[(j*extractNum)*srcWidth + (i*extractNum)];
data[i*width + j] = RGB(val*coefficient, val*coefficient, val*coefficient);
//data[i*width + j] = RGB(val/256, val/ 256, val / 256);
}
}
//create bitmap
bm.CreateBitmap(width, height, 1, 32, data);
//实现双缓冲,避免闪烁
MemDC->CreateCompatibleDC(pDC);
MemDC->SelectObject(&bm);
CRect rect;
GetDlgItem(IDC_STATIC)->GetWindowRect(&rect);
ScreenToClient(&rect);
pDC->SetStretchBltMode(COLORONCOLOR);//COLORONCOLOR
//rect.Width(), rect.Height() 此宽度与高度为实际显示的尺寸 既放大缩小的尺寸
pDC->StretchBlt(0, 0, rect.Width(), rect.Height(), MemDC, 0, 0, width, height, SRCCOPY);
////删除已经分配的内存空间
ReleaseDC(MemDC);
delete MemDC;
bm.DeleteObject();
delete[] data;
ReleaseDC(pDC);
}
catch(...)
{ }
}