在头文件里面添加:

Image* image;
GUID Guid ;
UINT frameCount;
UINT framePos;
ULONG_PTR gdiplusToken;


afx_msg void OnTimer(UINT_PTR nIDEvent);
void LoadGif();

需要依赖头文件:

#include <Gdiplusheaders.h>
#include <Gdiplus.h>

using namespace Gdiplus;

界面类的构造函数里面添加:

//启动gdi 要不然 Image::FromFile 会失败
    GdiplusStartupInput gdiplusStartupInput;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    image = NULL;
    frameCount = 0;
    framePos = 0;

添加定时器消息处理:

BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
    ON_WM_TIMER()
    ON_BN_CLICKED(IDC_BUTTON2, &CMFCApplication1Dlg::OnBnClickedButton2)
END_MESSAGE_MAP()

加载GIF文件:

void CMFCApplication1Dlg::LoadGif()
{
    WCHAR strModule[MAX_PATH * 2] = { 0 };
    GetModuleFileNameW(NULL, strModule, MAX_PATH * 2);
    ::PathRemoveFileSpecW(strModule);
    wsprintfW(strModule + wcslen(strModule), L"\\%s.gif", L"sVcodec");

    image = Image::FromFile(strModule);

    //获得有多少个维度,对于gif就一个维度
    UINT count = image->GetFrameDimensionsCount();
    GUID *pDimensionIDs = (GUID*)new GUID[count];
    image->GetFrameDimensionsList(pDimensionIDs, count);
    WCHAR strGuid[39];
    StringFromGUID2(pDimensionIDs[0], strGuid, 39);
    frameCount = image->GetFrameCount(&pDimensionIDs[0]);

    //delete[] pDimensionIDs;

    //获得各帧之间的时间间隔
    //先获得有多少个时间间隔,PropertyTagFrameDelay是GDI+中预定义的一个GIG属性ID值,表示标签帧数据的延迟时间
    UINT FrameDelayNums = image->GetPropertyItemSize(PropertyTagFrameDelay);
    PropertyItem* lpPropertyItem = new PropertyItem[FrameDelayNums];
    image->GetPropertyItem(PropertyTagFrameDelay, FrameDelayNums, lpPropertyItem);

    Guid = FrameDimensionTime;
    image->SelectActiveFrame(&Guid, framePos);


    
}

定时器处理:

void CMFCApplication1Dlg::OnTimer(UINT_PTR nIDEvent)
{
    if (image)
    {
        CWnd* pic = GetDlgItem(IDC_STATIC_PIC);
        Graphics gh(pic->GetDC()->m_hDC);

        CRect rect;
        GetDlgItem(IDC_STATIC_PIC)->GetClientRect(&rect);
        gh.DrawImage(image, 0, 0, rect.Width(), rect.Height());
        //gh.DrawImage(image, 0, 0, image->GetWidth(), image->GetHeight());

        //设置当前需要显示的帧数
        image->SelectActiveFrame(&Guid, framePos);
        framePos++;
        if (framePos == frameCount)
        {
            framePos = 0;
        }
    }

    CDialogEx::OnTimer(nIDEvent);
}

启动定时器:

SetTimer(0, 400, NULL);

    LoadGif();

需要依赖gdi 库:

gdiplus.lib

参考地址:https://blog.csdn.net/sz76211822/article/details/51507616

 

================================================================

VC2010使用GDI+在picture control控件里面显示图片

一、头文件添加

在项目的stdafx.h头文件里面添加如下头文件说明。

#include <gdiplus.h>    // GDI+库头文件,并在App头文件中声明相关变量
using namespace Gdiplus;

二、lib文件添加

项目-xx属性-链接器-输入-附加依赖性:输入gdiplus.lib

三、App文件对使用GDI+的初始化

xx.h头文件定义:ULONG_PTR m_gdiplusToken;

xx.cpp中在

BOOL CXXApp::InitInstance()

{

      //添加GDI+初始化说明

      // GDI+相关启动操作

      GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

}

四、打开图片操作函数

bool XX::ShowImageInBoxEC(CString pathname)
{
CWnd* pic = GetDlgItem(IDC_PIC_VIEW);    // 用此句,得到图片控件的CWnd,图片将被绘制在控件上,IDC_PIC_VIEW为picture control的控件ID
Graphics graphics(pic->GetDC()->m_hDC);


Image tempimage(pathname);
CRect rect;
GetDlgItem(IDC_PIC_VIEW)->GetClientRect(&rect);


graphics.DrawImage(&tempimage, 0, 0, rect.Width(), rect.Height());


return TRUE;
}

参考:https://blog.csdn.net/fengdingha/article/details/19479289

posted on 2019-06-20 19:25  瓦楞球  阅读(751)  评论(0编辑  收藏  举报