# [.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 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 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 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 on other sites
Quote:
 Original post by The ParrotPromit;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 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 on other sites
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

##### 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 on other sites
Ok, last try, I usually won;t do this in the first place: here

##### 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 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 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 on other sites
I haven't found a solution yet, but I thought I'd bump this thread as I'm sure there aught to be some solution... :)

##### Share on other sites
So... the methods/properties you need in the ImageCodecInfo class are?

and how do you use it? ie, do you pass it as a parametre to another object's method?

##### 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 on other sites
Another bump :)... if this leads to nothing I'm going to have to find another way.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628334
• Total Posts
2982147

• 9
• 24
• 9
• 9
• 13