Jump to content
  • Advertisement
Sign in to follow this  
The Parrot

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

This topic is 4900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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... :)

Share this post


Link to post
Share on other sites
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).

:(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!