Sign in to follow this  
myvraccount

PNG source code

Recommended Posts

myvraccount    235

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.

Share this post


Link to post
Share on other sites
myvraccount    235

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.

Share this post


Link to post
Share on other sites
jpetrie    13161
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?

Edited by Josh Petrie

Share this post


Link to post
Share on other sites
myvraccount    235

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.

Share this post


Link to post
Share on other sites
myvraccount    235

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.

Edited by myvraccount

Share this post


Link to post
Share on other sites
felipefsdev    360
Paint is a [b]terrible[/b] 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.

Share this post


Link to post
Share on other sites
myvraccount    235

felipefsdev, I realize it's just a junky freeware included thing, but it's always been fine for testing purposes when I'm dealing with bitmaps, and that's what I use it for, anyway.

 

ApochPiQ:  I've been looking at the link you gave me.  Is that WPF?  I've never used, am not sure if I even have, and can't seem to import the namespace they list for WPF (System.Windows.Media.Imaging doesn't seem to exist at all!).  If this is something that I need to include an extra DLL for, I'd really prefer to go the Image.FromFile and Image.Save route, like Josh suggested.  I can do it your way, if it works better, but it's just that I intend to distribute this, and I want it to have as few dependencies as possible.

 

UPDATE: Alright, I think I figured it out, but I'm not sure.  I just noticed that Josh supplied a link way on the right side of the screen that says (including PNG), and when I clicked that link, it says that I can use a property of the ImageFormat object to construct a specific ImageFormat to be a PNG, without having to call the constructor with a GUID (which I didn't know where to get anyway).  Do you think that's what I did wrong - I just didn't specify an ImageFormat, so it didn't know to convert it, and it left it untranslated?

Edited by myvraccount

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this