#pragma once class CDrawImg { public: CDrawImg(HWND wnd) { m_hbrush = CreateSolidBrush(RGB(0, 0, 0)); pBmpInfo = (BITMAPINFO *)chBmpBuf; pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pBmpInfo->bmiHeader.biPlanes = 1; pBmpInfo->bmiHeader.biCompression = BI_RGB; for (int i = 0; i < 256; i++) { pBmpInfo->bmiColors[i].rgbBlue = i; pBmpInfo->bmiColors[i].rgbGreen = i; pBmpInfo->bmiColors[i].rgbRed = i; pBmpInfo->bmiColors[i].rgbReserved = i; } GetClientRect(wnd, wrect); hdc = GetDC(wnd); } void Draw(int width, int height, int channels, uchar* data) { pBmpInfo->bmiHeader.biWidth = width; pBmpInfo->bmiHeader.biHeight = height; pBmpInfo->bmiHeader.biBitCount = 8 * channels; FillRect(hdc, wrect, m_hbrush); int DesX = 0; int DesY = wrect.Height(); int DesWidth = wrect.Width(); int DesHeight = wrect.Height(); double ratio = width / (double)height; int WidthRequest = ratio * wrect.Height(); int HeightRequest = wrect.Width() / ratio; if (wrect.Width() < WidthRequest) { DesHeight = DesWidth / ratio; DesY = (wrect.Height() - DesHeight) / 2 + DesHeight; } else if (wrect.Height() < HeightRequest) { DesWidth = DesHeight * ratio; DesX = (wrect.Width() - DesWidth) / 2; } ::SetStretchBltMode(hdc, COLORONCOLOR); ::StretchDIBits(hdc, DesX, DesY, DesWidth, -DesHeight, 0, 0, width, height, data, pBmpInfo, DIB_RGB_COLORS, SRCCOPY); } private: char chBmpBuf[2048]; BITMAPINFO *pBmpInfo; CRect wrect; HDC hdc; HBRUSH m_hbrush; };