使用GDI、MFC_GDI、GDI+绘制数组RGBA序列

学习ffmpeg时遇到一个问题,ffmpeg解码出RGB颜色后怎么绘制到屏幕上,于是将GDI、MFC_GDI、GDI+等方式都记录一下

1:注意按windows的要求,R、G、B、A顺序要调整为B、G、R、A 。

2:GDI不支持透明通道A,透明通道A的值读进去以后没有作用。想要支持透明通道,应使用GDI+方法。

GDI

void DrawBitmap(HWND hwnd, int x, int y, int nBmpWidth, int nBmpHeight,const unsigned char *pBmpData)
 {
	HBITMAP hBitmap = ::CreateBitmap(nBmpWidth, nBmpHeight, 1, 32, pBmpData);
	HDC hWndDc = ::GetDC(hwnd);
	HDC hMemDc = ::CreateCompatibleDC(hWndDc);
	HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDc, hBitmap);
	::BitBlt(hWndDc, x, y, nBmpWidth, nBmpHeight, hMemDc, 0, 0, SRCCOPY);
 
	::SelectObject(hMemDc, hOldBitmap);
	::DeleteObject(hBitmap);
	::DeleteDC(hMemDc);
	::ReleaseDC(hwnd, hWndDc);
}

 GDI+

#include <GdiPlus.h>
#pragma comment(lib,"Gdiplus.lib")
using namespace Gdiplus;
 
class GdiPlusInitialize
 {
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiPlusInitialize()
 {
		GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	}
	~GdiPlusInitialize()
 {
		GdiplusShutdown(gdiplusToken);
	}
	static GdiPlusInitialize Instance;
};
 
GdiPlusInitialize gdiplus_initialize_;

void DrawBitmap(HWND hwnd, int x, int y, int nBmpWidth, int nBmpHeight,unsigned char *pBmpData)
 {
	Bitmap bitmap(nBmpWidth, nBmpHeight, nBmpWidth * 4, PixelFormat32bppARGB, pBmpData);
	Graphics gps(hwnd);
	gps.DrawImage(&bitmap, x, y);
}

MFC_GDI


void DrawBitmap(CWnd *pWnd, int x, int y, int nBmpWidth, int nBmpHeight,const unsigned char *pBmpData)
 {
	CBitmap bitmap;CDC MemDc;
	bitmap.CreateBitmap(nBmpWidth, nBmpHeight, 1, 32, pBmpData);
	CDC *pWndDc = pWnd->GetDC();
	MemDc.CreateCompatibleDC(pWndDc);
	CBitmap *pOldBitmap = MemDc.SelectObject(&bitmap);
	pWndDc->BitBlt(x, y, nBmpWidth, nBmpHeight, &MemDc, 0, 0, SRCCOPY);
 
	MemDc.SelectObject(pOldBitmap);
	pWnd->ReleaseDC(pWndDc);pWndDc = NULL;
}

本文为@艺文笔记原创文章,转载请注明出处:https://www.xuwenyan.com/archives/1580,非常感谢!
上一篇:  下一篇: