🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #16873

Re: change wallpaper gif/jpg

#16873
From: Damon Chandler (TeamB)
Date: Wed, 23 Apr 2003 06:28
172 lines
5495 bytes
Hi Fernando,
> How can I convert gif/jpg to bmp?

The following function uses the IPicture interface to convert a GIF or JPEG
to a bitmap; it returns a TBitmap object which you can dump to a file via
TBitmap::SaveToFile()...

#include <ole2.h>
#include <olectl.h>
#include <memory>
Graphics::TBitmap* LoadImageFromFile(
   AnsiString filename
  )
{
  // open the file for shared reading
  HANDLE const hFile = CreateFile(
    filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
    );
  if (hFile == INVALID_HANDLE_VALUE)
  {
    throw EWin32Error("Error opening file.");
  }

  // allocate a global memory object of the file's size
  DWORD const dwSize = GetFileSize(hFile, NULL);
  HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, dwSize);
  if (hData == NULL)
  {
    CloseHandle(hFile);
    throw EWin32Error("Error allocating memory.");
  }

  // grab a pointer to the memory
  LPVOID pData = GlobalLock(hData);
  if (pData == NULL)
  {
    GlobalFree(hData);
    CloseHandle(hFile);
    throw EWin32Error("Error accessing memory.");
  }

  // read the data from the file
  DWORD dwRead = 0;
  bool read_ok = ReadFile(hFile, pData, dwSize, &dwRead, NULL);
  if (!read_ok  || (dwRead != dwSize))
  {
    GlobalUnlock(hData);
    GlobalFree(hData);
    CloseHandle(hFile);
    throw EWin32Error("Error reading file.");
  }

  // clean up
  GlobalUnlock(hData);
  CloseHandle(hFile);

  // create an IStream object on hData
  IStream* pStream;
  HRESULT hRes = CreateStreamOnHGlobal(hData, false, &pStream);
  if (FAILED(hRes))
  {
    GlobalFree(hData);
    throw EWin32Error("Failed to create IStream.");
  }

  // load the picture (create an IPicture object)
  IPicture* pPicture;
  hRes = OleLoadPicture(
    pStream, 0, false, IID_IPicture,
    reinterpret_cast<void**>(&pPicture)
    );
  if (FAILED(hRes))
  {
    pStream->Release();
    GlobalFree(hData);
    throw EWin32Error("Failed to create IPicture.");
  }

  // grab a handle to the Picture's palette, if present
  HPALETTE hPal = NULL;
  pPicture->get_hPal(reinterpret_cast<OLE_HANDLE*>(hPal));
  if (hPal != NULL)
  {
    int const num_entries =
      GetPaletteEntries(hPal, 0, 0, NULL);
    long const size = sizeof(LOGPALETTE) +
      (num_entries - 1) * sizeof(PALETTEENTRY);

    // copy the palette (because IPicture owns it)
    LPLOGPALETTE pLogPal =
      reinterpret_cast<LPLOGPALETTE>(GlobalAlloc(GPTR, size));
    if (pLogPal != NULL)
    {
      int const num_got = GetPaletteEntries(
        hPal, 0, num_entries, pLogPal->palPalEntry
        );
      pLogPal->palVersion = 0x300;
      pLogPal->palNumEntries = num_got;
      hPal = CreatePalette(pLogPal);
      GlobalFree(pLogPal);
    }
  }

  // retrieve the width and height of the picture
  SIZE SHiMetric = {0, 0};
  pPicture->get_Width(&SHiMetric.cx);
  pPicture->get_Height(&SHiMetric.cy);

  // convert HIMETRIC units to pixels
  HDC hScreenDC = GetDC(NULL);
  int const width = MulDiv(
    SHiMetric.cx, GetDeviceCaps(hScreenDC, LOGPIXELSX), 2540
    );
  int const height = MulDiv(
    SHiMetric.cy, GetDeviceCaps(hScreenDC, LOGPIXELSY), 2540
    );
  ReleaseDC(NULL, hScreenDC);

  // create a TBitmap object
  std::auto_ptr<Graphics::TBitmap> Bitmap(new Graphics::TBitmap());
  try
  {
    // set the bitmap's attributes
    Bitmap->PixelFormat = pf24bit;
    Bitmap->Width = width;
    Bitmap->Height = height;

    // grab a handle to the bitmap's memory DC
    HDC const hMemDC = Bitmap->Canvas->Handle;

    // associate the palette (if present) with the bitmap's DC
    HPALETTE hOldPal = NULL;
    if (hPal != NULL)
    {
      Bitmap->Palette = hPal;
      hOldPal = SelectPalette(hMemDC, hPal, false);
      RealizePalette(hMemDC);
    }

    // render the picture to the memory DC
    pPicture->Render(
      hMemDC, 0, 0, width, height, 0, SHiMetric.cy,
      SHiMetric.cx, -SHiMetric.cy, NULL
      );

    // clean up
    if (hOldPal != NULL)
    {
      SelectPalette(hMemDC, hOldPal, false);
    }
    pPicture->Release();
    pStream->Release();
    GlobalFree(hData);
  }
  catch (...)
  {
    // clean up
    pPicture->Release();
    pStream->Release();
    GlobalFree(hData);
    throw;
  }
  
  // retun the bitmap (caller assumes ownership)
  return Bitmap.release();
}  

HTH,
-- 
Damon (TeamB)
BCBCAQ--http://bcbcaq.bytamin-c.com
Technical Details
Message-ID: <3EA63274.D0D9367F@cornell.edu>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3ea2c223$1@newsgroups.borland.com> <3ea3164d$1@newsgroups.borland.com> <3ea319ae$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,fd7225d99dc4b60f
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-04-22 23:31:11 PST
Message-ID: <3EA63274.D0D9367F@cornell.edu>
Date: Wed, 23 Apr 2003 02:28:04 -0400
From: "Damon Chandler (TeamB)" <dmc27@cornell.edu>
X-Mailer: Mozilla 4.8 [en] (Windows NT 5.0; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: change wallpaper gif/jpg
References: <3ea2c223$1@newsgroups.borland.com> <3ea3164d$1@newsgroups.borland.com> <3ea319ae$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.92.234.87
X-Trace: newsgroups.borland.com 1051079210 24.92.234.87 (22 Apr 2003 23:26:50 -0700)
Lines: 172
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail