[.net] Saving to JPEG compression in .NET with C++

Started by
14 comments, last by The Parrot 18 years, 9 months ago
Hi, I'm saving some images to JPEG via the .NET Image class. The problem is that when I want to set my own compression rate. The code online says you can do that like thus;
bmp.Save(fileName, jpegCodecInfo, codecParams);
And to get the jpegCodecInfo you need to do this;
mimeType = "image/jpeg";
ImageCodecInfo GetEncoderInfo(string mimeType)
{
   int j;
   ImageCodecInfo[] encoders;
   encoders = ImageCodecInfo.GetImageEncoders();
   for(j = 0; j < encoders.Length; ++j)
   {
      if(encoders[j].MimeType == mimeType)
         return encoders[j];
   }
   return null;
}
Now, when I try to compile the C++ version of this I get an error in Visual Studio.NET which complained about the type of variable I use for the ImageCodecInfo[] array:
Texture.cpp: error C2691: 'unsigned char  __gc[]' : invalid type for __gc array element
This didn't make any sense at all, so after some further examination I found myself looking on a page where they explained this was an error with C++ and jaggered arrays and that it would be resolved in the next version of Visual Studio.NET. However, since I do not want to buy a new version for this I wondered if someone could help me out either with an solution or with a simple compiled codefile in which this function is written (in any language I can use from C++). In await of your replies ;) -Jeroen
--Jeroen Stout - WebsiteCreator of-Divided
Advertisement
I don't know how to fix the problem with the Image class, but since it is still C++ you might want to try interfacing with an external library, for example IJG's libjpeg. It'll be a pain to do, but it's better than nothing I suppose.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
rewrite just that bit in C#, build it as a dll, import it into the C++ program? It's the only workaround I can think of, outside of the previous suggestion.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Promit;
That's a good last-resort, but I'm using the .NET framework for writing to streams etc. so that'd be slower and harder. Thanks, though.

capn_midnight;
Ok, I'll try that - I can't compile C# but I think I have a friend who can, I'll ask him.
--Jeroen Stout - WebsiteCreator of-Divided
Quote:Original post by The Parrot
Promit;
That's a good last-resort, but I'm using the .NET framework for writing to streams etc. so that'd be slower and harder. Thanks, though.

capn_midnight;
Ok, I'll try that - I can't compile C# but I think I have a friend who can, I'll ask him.

If you are using .Net, then you can compile C#. The compiler is included in the framework that you had to install to run .Net apps.
Turring Machines are better than C++ any day ^_~
Wow, I didn't know that - could you give me some pointers on how to write it, then? In my searches I found very little on that as well.

Cheers!
--Jeroen Stout - WebsiteCreator of-Divided
I was feeling nice, so here is a library with a single class, "JpegSaver.FileSaver", that has a function for saving Jpegs with compression.

Here it is
Turring Machines are better than C++ any day ^_~
Whilst I greatly appreciate this, I unfortionately can't use this as I save to a stream (I'll write over 10,000 files in the end so I save them into one file). If you could make a class which returns the appropriate ImageCodecInfo when the right string is given I'd greatly value it... :)
--Jeroen Stout - WebsiteCreator of-Divided
Ok, last try, I usually won;t do this in the first place: here
Turring Machines are better than C++ any day ^_~
Too bad, it just gives the same error when I actually call the function - the code which initiates the class just worked fine but when I call the function it returns the error and points to a function in a file "xmemory" called "allocate". In other words, I think Studio.NET is just importing the code from the .dll somehow (or at least this wrapper didn't work, it seems).

:(
--Jeroen Stout - WebsiteCreator of-Divided

This topic is closed to new replies.

Advertisement