Discuz! Board
标题:
[代码|Code]GDI Load Frame
[打印本页]
作者:
Jerry小坏
时间:
2017-7-14 16:43
标题:
[代码|Code]GDI Load Frame
通过以下代码你可以选择画哪一张GIF的帧到HUD上[]
You can select to draw which frame of GIF onto HUD by this code.
loadimage.h:
unsigned char * loadNormalFormat (const wchar_t *filename, int *width, int *height, int frame);
复制代码
loadimage.cpp:
#include <Windows.h>
#include <GdiPlus.h>
#include "loadimage.h"
#pragma comment (lib, "gdiplus.lib")
unsigned char* loadNormalFormat (const wchar_t *filename, int *width, int *height, int frame)
{
// 启动GDI+
Gdiplus::GdiplusStartupInput gdipSIn;
unsigned long gdipToken;
Gdiplus::GdiplusStartup (&gdipToken, &gdipSIn, NULL);
// 加载图像文件
Gdiplus::Bitmap *pBitmap = new Gdiplus::Bitmap (filename);
// 获得图像高宽
unsigned int bitmapWidth = pBitmap->GetWidth ();
unsigned int bitmapHeight = pBitmap->GetHeight ();
if (bitmapWidth <= 0 || bitmapHeight <= 0)
{
delete pBitmap;
// 结束GDI+
Gdiplus::GdiplusShutdown (gdipToken);
return NULL;
}
/* Load Frame Here */
UINT count;
count = pBitmap->GetFrameDimensionsCount();
GUID* pDimID = new GUID[count];
pBitmap->GetFrameDimensionsList(pDimID,count);
int FrameCount = pBitmap->GetFrameCount(&pDimID[0]);
pBitmap->SelectActiveFrame(&pDimID[0], frame);// Set Frame
// 获取图像元数据构造GlImage
Gdiplus::Rect bitmapArea (0, 0, bitmapWidth, bitmapHeight);
Gdiplus::BitmapData bitmapData;
pBitmap->RotateFlip(Gdiplus::Rotate180FlipX); // 因为位图的存储是上下翻转的,这里要绕X轴转180度[]
pBitmap->LockBits (&bitmapArea, Gdiplus::ImageLockModeRead, pBitmap->GetPixelFormat (), &bitmapData);
const BYTE *imageData = (BYTE *)bitmapData.Scan0;
int stride = bitmapData.Stride;
unsigned char *glImageData = NULL;
if (imageData != 0)
{
glImageData = new unsigned char[bitmapWidth * bitmapHeight * 4];
const int destR = 0, destG = 1, destB = 2, destA = 3;
int srcR, srcG, srcB, srcA, bytesPerPixel;
// 根据像素格式设置初始值[]
switch (bitmapData.PixelFormat)
{
case PixelFormat24bppRGB:
{
srcR = 2; srcG = 1; srcB = 0; bytesPerPixel = 3;
break;
}
case PixelFormat32bppARGB:
{
srcA = 3; srcR = 2; srcG = 1; srcB = 0; bytesPerPixel = 4;
break;
}
default: // 图片的像素格式不支持
{
delete [] glImageData;
pBitmap->UnlockBits (&bitmapData);
delete pBitmap;
// 结束GDI+
Gdiplus::GdiplusShutdown (gdipToken);
return NULL;
}
}
// 复制及转换图像数据[]
for (unsigned int y = 0; y < bitmapHeight; ++y)
{
for (unsigned int x = 0; x < bitmapWidth; ++x)
{
glImageData[(y * bitmapWidth + x) * 4 + destR] = imageData[((bitmapHeight - 1 - y) * stride) + x * bytesPerPixel + srcR];
glImageData[(y * bitmapWidth + x) * 4 + destG] = imageData[((bitmapHeight - 1 - y) * stride) + x * bytesPerPixel + srcG];
glImageData[(y * bitmapWidth + x) * 4 + destB] = imageData[((bitmapHeight - 1 - y) * stride) + x * bytesPerPixel + srcB];
if (bytesPerPixel == 4)
{
glImageData[(y * bitmapWidth + x) * 4 + destA] = imageData[((bitmapHeight - 1 - y) * stride) + x * bytesPerPixel + srcA];
}
else
glImageData[((bitmapHeight - 1 - y) * stride) * 4 + destA] = 255;
}
}
}
pBitmap->UnlockBits (&bitmapData);
delete pBitmap;
// 返回图像尺寸
*width = bitmapWidth;
*height = bitmapHeight;
// 结束GDI+
Gdiplus::GdiplusShutdown (gdipToken);
return glImageData;
}
复制代码
欢迎光临 Discuz! Board (https://esfbbs.foxnyx.top/)
Powered by Discuz! X3.2