Sign in to follow this  
The Parrot

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

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
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
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
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
Yes I seem to be getting the same problems... which is really really odd.

I've written a wrapper class myself, in C#, then done a c++ app like so:


ImageCodecInfo* ici = Helper::ImageCodecInfoRetrieve::GetImageCodecInfo(S"image/jpeg");

if (ici!=0)
Console::WriteLine(ici->ToString());



and I get the same error on the ToString() call... Very very odd.

What methods do you need? those could easily be wrapped up too...

here is the C# wrapper btw:


using System;
using System.Drawing.Imaging;

namespace Helper
{
public class ImageCodecInfoRetrieve
{
public ImageCodecInfoRetrieve()
{
}

public static ImageCodecInfo GetImageCodecInfo(string mime)
{
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(int j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mime)
return encoders[j];
}
return null;
}
}
}

Share this post


Link to post
Share on other sites
Same odd-ly placed error as I'm getting (only on a different spot); and I'm trying to use exactly the same function.

I never got to making the wrapper as quite frankly I haven't got a clue how to make such a thing in the Developer version of VS.NET that I have :|...

It's odd that the wrappers are named as a potential solution but don't work nontheless - if this problem really persists I might contact Microsoft about it (well, it's their bug, really) and might get free help from them (though them being Microsoft that is at least not something you can actually count on).

Share this post


Link to post
Share on other sites
The only thing I need is to get a ImageCodecInfo class with the JPEG-codec information, which I then pass to the Image::Save(...) function to save it to a stream - using an image I opened, of course.

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