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

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

## 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).

:(

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632591
• Total Posts
3007250

×