Animated Image in Win32

10,256

Solution 1

Not sure if GDI+ could be considered as native win32. In case you can use it check the following example: CodeProject

Solution 2

You could use the Animation Control. You would have to convert your .gif to an .avi though.

Solution 3

Its very easy to use GdiPlus to load a variety of image formats including jpeg, gif (animated), png and so on.

This code demonstrates how to quickly load a single frame of an image into an HBITMAP :-

#include <gdiplus.h>
#pragma comment(lib,"gdiplus.lib")

using namespace Gdiplus;

HBITMAP LoadImageWithGdiPlus(LPCTSTR pszPngPath)
{
  Image image(pszPngPath);
  int width = image.GetWidth();
  int height = image.GetHeight();

  BITMAPINFO bmi;
  bmi.bmiHeader.biBitCount = 32;
  bmi.bmiHeader.biClrImportant = 0;
  bmi.bmiHeader.biClrUsed = 0;
  bmi.bmiHeader.biCompression = BI_RGB;
  bmi.bmiHeader.biHeight = height;
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biSize = sizeof (bmi.bmiHeader);
  bmi.bmiHeader.biSizeImage = 0; //calc later
  bmi.bmiHeader.biWidth = width;
  bmi.bmiHeader.biXPelsPerMeter = 0;
  bmi.bmiHeader.biYPelsPerMeter = 0;
  BYTE* pBmp = NULL;
  HBITMAP hbm = CreateDIBSection(NULL,&bmi,DIB_RGB_COLORS,(void**)&pBmp,NULL,0);
  HDC hdc = CreateCompatibleDC(NULL);
  HGDIOBJ hobj = SelectObject(hdc,hbm);

  Graphics graphics(hdc);
  graphics.DrawImage(&image,0,0);

  SelectObject(hdc,hobj);
  DeleteDC(hdc);
  return hbm;
}
Share:
10,256

Related videos on Youtube

frbry
Author by

frbry

Updated on April 18, 2022

Comments

  • frbry
    frbry about 2 years

    How can i put an animated GIF to my dialog in my native Win32 application?

    I have a loading indicator, and a loading process.

    Thanks :-)

  • Mark Ransom
    Mark Ransom about 14 years
    The follow-up question is "How do you convert a .gif to a .avi?" Include that in your answer and it would be perfect.
  • Fozi
    Fozi about 14 years
    Since Imagemagick failed, all I can offer is what google brought up. (which is not too bad - I searched for convert gif to avi) Maybe someone else has a good idea?
  • Mecanik
    Mecanik over 5 years
    Change to "HBITMAP LoadImageWithGdiPlus(LPWSTR pszPngPath)" for UNICODE Projects. Plus you are missing: docs.microsoft.com/en-gb/windows/desktop/api/gdiplusinit/….