19

Can I convert a bitmap to PNG in memory (i.e. without writing to a file) using only the Platform SDK? (i.e. no libpng, etc.).

I also want to be able to define a transparent color (not alpha channel) for this image.

The GdiPlus solution seems to be limited to images of width divisible by 4. Anything else fails during the call to Save(). Does anyone know the reason for this limitation and how/whether I can work around it?

Update: Bounty

I'm starting a bounty (I really want this to work). I implemented the GDI+ solution, but as I said, it's limited to images with quad width. The bounty will go to anyone who can solve this width issue (without changing the image dimensions), or can offer an alternative non-GDI+ solution that works.

7
  • to get you started, i found an example to save an bitmap as png with gdi+: dotnet-snippets.de/dns/gdi-speichern-eines-png-SID814.aspx maybe it's useful Dec 14, 2008 at 17:39
  • This solution works for me, even with images where width is not divisible by 4. I'm on Windows Vista and using Visual C++ 2008.
    – friol
    Feb 11, 2009 at 19:38
  • Why don't you want to use libpng? It has a very permissive license. Feb 11, 2009 at 20:10
  • Does libpng do in-memory encoding? (or just to files?) Feb 12, 2009 at 11:20
  • 1
    You can hook libpng's output using png_set_write_fn and do whatever you want with it.
    – timday
    Feb 12, 2009 at 20:35

8 Answers 8

21

LodePNG (GitHub) is a lib-less PNG encoder/decoder.

14
+100

I read and write PNGs using libpng and it seems to deal with everthing I throw at it (I've used it in unit-tests with things like 257x255 images and they cause no trouble). I believe the API is flexible enough to not be tied to file I/O (or at least you can override its default behaviour e.g see png_set_write_fn in section on customization)

In practice I always use it via the much cleaner boost::gil PNG IO extension, but unfortunately that takes char* filenames and if you dig into it the png_writer and file_mgr classes in its implementation it seem pretty tied to FILE* (although if you were on Linux a version using fmemopen and in-memory buffers could probably be cooked up quite easily).

5
  • +1 thanks. May I know how to use Boost::GIL with PNG IO? I have no clue about integration and making use of that.
    – Viet
    Feb 26, 2010 at 14:12
  • 1
    Boost GIL includes an "IO extension" which currently includes support for basic reading and writing to PNG files but doesn't provide enough hooks to read/write in-memory streams as the original poster was asking about. There seems to be a new IO extension (google something like "boost GIL new IO extension" to find various postings) under development which looks like it will give a lot more flexibility in this area.
    – timday
    Mar 2, 2010 at 20:29
  • 1
    libpng, while a good solution, seems to violate the question's constraint of "using only the Platform SDK". Oct 31, 2011 at 21:35
  • how do I to install this lib please? Or do I need to simply get the Source code? Mar 28, 2014 at 0:21
  • 1
    @Lucie: I also struggled with finding a windows version; see my other question stackoverflow.com/questions/8669163/… . Windows development drives me bananas with this sort of stuff, compared with Linux where you can just apt-get install libpng-dev and be good to go in seconds.
    – timday
    Mar 28, 2014 at 12:55
12

On this site the code shows how convert a bitmap to PNG writing it to a file: http://dotnet-snippets.de/dns/gdi-speichern-eines-png-SID814.aspx. Instead of writing to a file, the Save method of Bitmap also supports writing to a IStream (http://msdn.microsoft.com/en-us/library/ms535406%28VS.85%29.aspx). You can create a Stream backed up by memory using the CreateStreamOnHGlobal API function. (http://msdn.microsoft.com/en-us/library/aa378980%28VS.85%29.aspx). The used library, GDI+, is included in Windows up from WindowsXP, and works in Windows up from Windows98. I've never done something with it, just googled around. Looks like you can use that, though.

1
  • 1
    What about the GDI+ problem I mentioned in the original question? Nov 1, 2011 at 5:50
8

The CImage class (ATL/MFC) supports saving into PNG format. Like the GDI+ solution, it also supports saving to a stream. Here's some code I use to save it to a CByteArray:

CByteArray baPicture;
IStream *pStream = NULL;
if (CreateStreamOnHGlobal(NULL, TRUE, &pStream) == S_OK)
{
    if (image.Save(pStream, Gdiplus::ImageFormatPNG) == S_OK)
    {
    ULARGE_INTEGER ulnSize;
        LARGE_INTEGER lnOffset;
        lnOffset.QuadPart = 0;
        if (pStream->Seek(lnOffset, STREAM_SEEK_END, &ulnSize) == S_OK)
        {
            if (pStream->Seek(lnOffset, STREAM_SEEK_SET, NULL) == S_OK)
            {                       
                baPicture.SetSize(ulnSize.QuadPart);
                ULONG ulBytesRead;
                pStream->Read(baPicture.GetData(), ulnSize.QuadPart, &ulBytesRead);
            }
        }
    }
}
pStream->Release();

I don't know if you'd want to use ATL or MFC, though.

4
  • 1
    Alas, there's no ATL/MFC in my project. Feb 12, 2009 at 11:14
  • ok... maybe the stream save part could be useful, it should be similar to the one using GDI+
    – djeidot
    Feb 12, 2009 at 12:08
  • This helped me for something I was working on, +1 for that at least.
    – Aardvark
    Feb 12, 2009 at 15:39
  • I'm pretty sure this uses libpng
    – Adam Tegen
    Feb 12, 2009 at 23:42
6

I've used GDI+ for saving a bitmap as a PNG to a file. You should probably check out the MSDN info about GDI+ here and in particular this function GdipSaveImageToStream.

This tutorial here will probably provide some help as well.

5

GDI's (old school, non-plus) has a GetDIBits method that can be asked to output bits using PNG compression (BITMAPINFOHEADER::biCompression == BI_PNG). I wonder if this could be used to create a PNG file? Using GetDIBits to write standard bitmap files is complicated enough - so i suspect this would be even more difficult.

1
3

If you want to only use Windows APIs, WIC is the way to accomplish this, and it supports both Bitmaps and PNGs.

2
  • Are you certain it doesn't suffer from the same weird width limitation as GDI+? Feb 12, 2009 at 11:19
  • 1
    No, not certain - but WIC is definitely more modern and kept up-to-date than GDI+
    – Ana Betts
    Feb 12, 2009 at 17:02
2

It would probably be better to use a library instead of reinventing the wheel yourself.

Look into freeImage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.