GdiPlus和gdi函数

StretchBlt

MaskBlt

BitBlt

    using namespace Gdiplus;	
    IDirect3DTexture9* mTexture;

	Bitmap b(32, 32, PixelFormat32bppARGB);
	Gdiplus::Graphics g(&b);
	g.Clear(Color::MakeARGB(88, 255, 255, 255));
	Pen p(Color::Red);
	g.DrawLine(&p, 0, 0, b.GetWidth()-1, b.GetHeight()-1);
	g.DrawLine(&p, 0, b.GetHeight()-1, b.GetWidth()-1, 0);
	
	unsigned int pixelSize = Gdiplus::GetPixelFormatSize(PixelFormat32bppARGB)/8;

	//save bitmap for comparison
	CLSID pngClsid;
	GetEncoderClsid(L"image/bmp", &pngClsid);
	b.Save(L"test_bit.bmp", &pngClsid, NULL);
	UINT width = b.GetWidth();
	UINT height = b.GetHeight();

	HRESULT hr = D3DXCreateTexture(pDevice, width, height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &mTexture);

	D3DLOCKED_RECT lockedRect;
	mTexture->LockRect(0, &lockedRect, 0, 0);
	unsigned char* pDestPixels = (unsigned char*)lockedRect.pBits;

	BitmapData bitmapData;
	Rect rect(0, 0, width, height);
	b.LockBits(&rect, ImageLockModeRead, b.GetPixelFormat(), &bitmapData);
	unsigned char* pSourcePixels = (unsigned char*)bitmapData.Scan0;
	
	for (int y = 0; y < height; ++y)
	{
		// copy a row
		memcpy(pDestPixels, pSourcePixels, width * pixelSize);   // 4 bytes per pixel

		// advance row pointers
		pSourcePixels += bitmapData.Stride;
		pDestPixels += lockedRect.Pitch;
	}
	b.UnlockBits(&bitmapData);
	
	mTexture->UnlockRect(0);

	if (hr == S_OK)
	{
		D3DXSaveTextureToFile(L"test2.bmp", D3DXIFF_BMP, mTexture, 0);
		mTexture->Release();
	}

	DWORD* pBitmapBits;                                                        //  创建一个位图
	BITMAPINFO bitmapInfo;
	ZeroMemory(&bitmapInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
	bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bitmapInfo.bmiHeader.biWidth = width;
	bitmapInfo.bmiHeader.biHeight = height;
	bitmapInfo.bmiHeader.biPlanes = 1;
	bitmapInfo.bmiHeader.biCompression = BI_RGB;
	bitmapInfo.bmiHeader.biBitCount = 32;

	HBITMAP hBitmap;	
	b.GetHBITMAP(Color::Black, &hBitmap);

	BITMAP bmp;
	GetObject(hBitmap, sizeof(BITMAP), &bmp);
	int  blength;                                  // 整个hBitMap文件字节长度
	blength = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bmp.bmWidthBytes * bmp.bmHeight;
	BYTE* buffer = new BYTE[blength];

	BITMAPINFOHEADER bi;
	bi.biSize = sizeof(BITMAPINFOHEADER);
	bi.biWidth = bmp.bmWidth;
	bi.biHeight = bmp.bmHeight;
	bi.biPlanes = 1;
	bi.biBitCount = bmp.bmBitsPixel;
	bi.biCompression = BI_RGB;
	bi.biSizeImage = 0;
	bi.biXPelsPerMeter = 0;
	bi.biYPelsPerMeter = 0;
	bi.biClrImportant = 0;
	bi.biClrUsed = 0;

	BITMAPFILEHEADER bmfHdr;
	bmfHdr.bfType = 0x4D42;  //  "BM"   
	bmfHdr.bfSize = blength;
	bmfHdr.bfReserved1 = 0;
	bmfHdr.bfReserved2 = 0;
	bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);

	memcpy(buffer, &bmfHdr, sizeof(BITMAPFILEHEADER));
	memcpy(&buffer[sizeof(BITMAPFILEHEADER)], &bi, sizeof(BITMAPINFOHEADER));
	memcpy(&buffer[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)],
		bmp.bmBits, blength - sizeof(BITMAPFILEHEADER) - sizeof(BITMAPINFOHEADER));


	// 将刚才构建好的bmp数据,转成IDirect3DTexture9*  的纹理  
	hr = D3DXCreateTextureFromFileInMemory(pDevice, buffer, blength, &mTexture);
	
	D3DXSaveTextureToFile(L"test3.bmp", D3DXIFF_BMP, mTexture, 0);

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值