Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Saving PNG with Windows API and C/C++

1,378 views
Skip to first unread message

Rene Grothmann

unread,
Nov 3, 2007, 9:34:05 AM11/3/07
to
I have a huge application for windows, written in the past 20 years. I
am using the Win32 API (no MFC) currently (as introduced with Windows
98), and a home made bitmap saving routine. However, I want to save a
memory bitmap to PNG, and also load PNG files to memory bitmaps.
Currently, the development is based on Microsoft Visual Studio. The
language is C++.

What would be my easiest option? My dream come true would be a ready
to use code to do the jobs. It should be free and open source. The
Windows API does not seem to include functions for this, or I did not
find them. MFC contains an image class, but it seems overkill, and I
do not know about NT compliance using GDI+.

Thanks for any hints. And sorry, if I missed an obvious solution.

Christian ASTOR

unread,
Nov 3, 2007, 11:01:07 AM11/3/07
to
Rene Grothmann wrote:
> I have a huge application for windows, written in the past 20 years. I
> am using the Win32 API (no MFC) currently (as introduced with Windows
> 98), and a home made bitmap saving routine. However, I want to save a
> memory bitmap to PNG, and also load PNG files to memory bitmaps.
> Currently, the development is based on Microsoft Visual Studio. The
> language is C++.

You can convert BMP <=> PNG with SHConvertGraphicsFile() (recent
shlwapi.dll)
or use GDI+ (redistributable GDIPlus.dll (>= NT 4.0 SP6))
or libpng ...

Grzegorz Wróbel

unread,
Nov 3, 2007, 11:39:33 AM11/3/07
to
Christian ASTOR wrote:
>
> You can convert BMP <=> PNG with SHConvertGraphicsFile() (recent shlwapi.dll)

I can't find any reference to this in MSDN.

--
Grzegorz Wróbel
http://www.4neurons.com/
677265676F727940346E6575726F6E732E636F6D

Christian ASTOR

unread,
Nov 3, 2007, 12:25:50 PM11/3/07
to
Grzegorz Wróbel wrote:

> Christian ASTOR wrote:
>>
>> You can convert BMP <=> PNG with SHConvertGraphicsFile() (recent
>> shlwapi.dll)
>
> I can't find any reference to this in MSDN.

It's exported with ordinal 488 =>

{
typedef HRESULT (WINAPI*PSHCGF) (IN LPCWSTR pSrcFileName, IN LPCWSTR
pDestFileName, IN DWORD dwFlags) ;
HINSTANCE hInstance = LoadLibrary("shlwapi.dll");
if (hInstance)
{
PSHCGF pSHCGF = (PSHCGF)GetProcAddress(hInstance, MAKEINTRESOURCE(488));
if (pSHCGF != NULL)
{
HRESULT hr = pSHCGF(L"foo.bmp", L"foo.png", 1);
if (SUCCEEDED(hr))
MessageBox(NULL, "File converted", "Information", MB_OK |
MB_ICONINFORMATION);
}
FreeLibrary(hInstance);
}
}

Grzegorz Wróbel

unread,
Nov 3, 2007, 12:39:26 PM11/3/07
to

What are the possible values of the third parameter (flags)? Will the
function deal with alpha-channeled pngs? What about 8bit pngs with 1bit
transparency. Will transparency information be preserved?

Christian ASTOR

unread,
Nov 3, 2007, 1:08:00 PM11/3/07
to
Grzegorz Wróbel wrote:

> What are the possible values of the third parameter (flags)?

1 is just to overwrite destination file.

> Will the function deal with alpha-channeled pngs? What about 8bit pngs with 1bit
> transparency. Will transparency information be preserved?

In fact, it uses IShellImageData interface.

Grzegorz Wróbel

unread,
Nov 3, 2007, 6:23:36 PM11/3/07
to
Christian ASTOR wrote:
> Grzegorz Wróbel wrote:
>
>> What are the possible values of the third parameter (flags)?
>
> 1 is just to overwrite destination file.
>

Any others?

>> Will the function deal with alpha-channeled pngs? What about 8bit pngs
>> with 1bit transparency. Will transparency information be preserved?
>
> In fact, it uses IShellImageData interface.

It doesn't really answer my question. I will have to check myself I
guess. I would prefer something that can convert files in memory and
documented of course. But still this function would be the only really
*native* support for the png files I am aware of. If it doesn't preserve
transparency while converting to .bmp file (and vice versa), it won't be
of that much value, though.

Grzegorz Wróbel

unread,
Nov 4, 2007, 7:02:46 PM11/4/07
to
Grzegorz Wróbel wrote:
>>> Will the function deal with alpha-channeled pngs? What about 8bit
>>> pngs with 1bit transparency. Will transparency information be preserved?
>
> I will have to check myself I guess.

It does preserve transparency from png when converting to bmp, both
alpha channeled and 1-bit. Pity it doesn't preserve transparency from
gif files, it probably uses old OLE code for those.

Gernot Frisch

unread,
Nov 5, 2007, 4:17:36 AM11/5/07
to

> What would be my easiest option? My dream come true would be a ready
> to use code to do the jobs. It should be free and open source. The
> Windows API does not seem to include functions for this, or I did
> not
> find them. MFC contains an image class, but it seems overkill, and I
> do not know about NT compliance using GDI+.

A very easy way to read/write these is the MFC/ATL class CImage.
You could write a wrapper dll that works with a HBITMAP for exchange,
maybe?


0 new messages