๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: change wallpaper gif/jpg

#16823
From: Fernando Alvarez
Date: Fri, 25 Apr 2003 10:55
177 lines
6043 bytes
Cool!!!!. It works fine fine fine!!!!! Thank you, thank you very much.

"Damon Chandler (TeamB)" <dmc27@cornell.edu> escribi๏ฟฝ en el mensaje
news:3EA63274.D0D9367F@cornell.edu...
> 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: <3ea91444@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!news.maxwell.syr.edu!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> <3EA63274.D0D9367F@cornell.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 10cc67,fd7225d99dc4b60f
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-04-25 04:02:38 PST
From: "Fernando Alvarez" <ferjat@lycos.es>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3ea2c223$1@newsgroups.borland.com> <3ea3164d$1@newsgroups.borland.com> <3ea319ae$1@newsgroups.borland.com> <3EA63274.D0D9367F@cornell.edu>
Subject: Re: change wallpaper gif/jpg
Date: Fri, 25 Apr 2003 12:55:53 +0200
Lines: 179
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 81.203.10.123
Message-ID: <3ea91444@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1051268164 81.203.10.123 (25 Apr 2003 03:56:04 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail