PNG source code

Started by
10 comments, last by myvraccount 6 years, 10 months ago

Does anyone know where I can get some C# open source code to compress PNG files from a 24-bit bitmap, and decompress them also?

If there's something built into the libraries, I'll use that, but I don't know of any.

Otherwise, I'd really like something that I can just copy and paste into my program, without having to import any DLLs or anything else, because I intend to distribute this, and I'd like it to be with as few dependencies as possible.

Also, if you have an open source Huffman compressor for a different but related issue, that would be nice as well, but I can manage that pretty easily manually, if need be.

Advertisement
Literally clicked on the first Google result.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Image.FromFile will give you an Image from a file of one of the standard supported types (.bmp is one of those). Image.Save can be used to save a file into a stream (including a file-backed stream) in a given format (including PNG).

Oh, thanks!

ARGH!

First, I tried loading in a BMP and converting it and storing it as a PNG. It stored a PNG, but it was exactly the same size as the BMP! Not very efficient compression, huh?

Then I tried loading a JPG and storing it as a PNG, and that kept it the same exact size! I thought, a PNG as small as a JPG? No way! But I opened it, and it displays fine in Paint! So then I made it convert that file to a BMP, and it still kept it the same size as the original JPG! So now I knew something was wrong, but when I opened it in Paint, it still displayed!

When I tried to save, however, by default it wanted to save as a JPG, so I saved it as a different BMP file, and that made a new one that was much larger (like the size I'd expect the BMP to actually be). I opened that in Paint, and when I tried to save it, it said save as BMP format by default. But when I opened the other BMP that's the size of a JPG and tried to save, it said to save as JPG as default.

So my theory is that Paint is reading the file as whatever it seems to be, supposedly based on the header, rather than whatever the file type actually is! This is stupid, because any program that doesn't do that wouldn't even be compatible with these incorrectly converted files.

It also means that my method of conversion isn't actually compressing anything, but just copying data to a new file and renaming it with a new extension!

There's a class called ImageConverter in system.drawing, but I'm not sure if that's what I should use, or how to use it. The constructor expects a GUID, which I don't have.

So my theory is that Paint is reading the file as whatever it seems to be, supposedly based on the header, rather than whatever the file type actually is! This is stupid, because any program that doesn't do that wouldn't even be compatible with these incorrectly converted files.

This is not "stupid," this is a very common practice. Trusting the "file type" (which is to say, the extension, because Windows is not Classic MacOS with type and creator codes) is just as unreliable a method of determine what a file actually is as is reading the first few bytes of the file and guessing based on known file format headers and the like. Trusting the extension is perhaps less reliable, in fact, because non-technical users are surprisingly good at turning off "safety features" like the hiding of extensions and then changing those extensions accidentally or on purpose (thinking it will magically change the file data).

It's arguable that a program should support both methods of loading a file, and provide a mechanism to forcibly open a file as one specific thing, and maybe Paint doesn't do that, but that's neither here nor there.

It also means that my method of conversion isn't actually compressing anything, but just copying data to a new file and renaming it with a new extension!

I'd verify that your files are saving to the locations you think they are, and that you're opening the files you think you are, and have passed the correct values to the correct parameters. The Image functions certainly work and facilitate the conversion of file formats.

There's a class called ImageConverter in system.drawing, but I'm not sure if that's what I should use, or how to use it. The constructor expects a GUID, which I don't have.

The constructor does not take a GUID, nor do any of the members listed in the documentation. Which version of the API are you looking at, and are you sure you're looking at the same ImageConverter class?

I'd verify that your files are saving to the locations you think they are, and that you're opening the files you think you are, and have passed the correct values to the correct parameters. The Image functions certainly work and facilitate the conversion of file formats.

I'm certain they're the correct files because they were created as I ran my program and then the modified date has updated each time.

As for the correct values and parameters, I think I'm doing it correctly. I just called Image.FromFile with the file name, and stored the result in a Bitmap, then called Image.Save with the file name where I wanted to save it. The file I loaded and the file I saved were of different extensions.

The constructor does not take a GUID, nor do any of the members listed in the documentation. Which version of the API are you looking at, and are you sure you're looking at the same ImageConverter class?

Oh, crap. Okay, that's my bad. I mis-remembered something. I was thinking of ImageFormat. Sorry.

But as for ImageConverter, it looks like it mostly just has functions for telling me what it can or cannot convert, but no way to actually set those flags, and some functions to convert between Image objects and other objects that are not even Image type. I'm not sure this is even relevant to what I'm trying to do at all; I just found it and thought that it might be relevant.

All I want is when I save an image, for it to automatically convert the data to the file format I want, and not just change the extension.

Have you tried the encoding APIs I linked earlier? If so, what specific errors did you get when trying to use the API?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Oh, sorry ApochPiQ. I didn't even realize that was a link, because for some reason, it appeared extremely dark on my monitor, and looked like the rest of the text. I'll check it now, thanks.

Paint is a terrible program for art. Not just lack features artwise, but also lack support for some formats (sometimes can read them, but not save). As Josh said, it's likely that all your files are actually BMP, just with a different extension name (.jpg, .png). Extensions don't determine the actual content of the file. Use a proper graphics editor. These are some free options with good support for PNG (and many other formats): GIMP, Aseprite, GrafX2.

This topic is closed to new replies.

Advertisement