Ok so after I got the cropping of a image done (see my log Cropping image) and had the “A generic error occurred in GDI+.” Error. By using:
using(Bitmap bitmap = new Bitmap(imageRigth))
{
bitmap.Save(MapPath(fileName), imageRigth.RawFormat);
}
After that I was just uploading some picture for some more testing but the thing I was seeing was that the picture’s where 300 kb for just a 250x300 picture witch is large So I needed to get the stuff compressed here is how to do it.
First why is the picture saved now so big? Very easy it is saved as RawFormat (bmp).
How to save a compressed version this can be done like this:
public void Save(Stream, ImageCodecInfo, EncoderParameters);
So we have the stream witch is the image now for the “ImageCodecInfo”.
For the ImageCodecInfo I have the following function:
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(int j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
You could get the jpg codec with "image/jpeg" as mimeType, “image/gif” for gif , “image/tiff” for tif or “image/png” for png.
That’s really it I never got the hang on what it exactly dues only that it go’s for the codec method that you put in.
Now for the EncoderParameters part this is the picture encoder , there can be multiple encoders in a “EncoderParameters“. I have the following function that I use for the encoder:
private static EncoderParameters Encode()
{
EncoderParameters myEncoderParameters = new EncoderParameters();
EncoderParameter myEncoderParameter = new EncoderParameter(Encoder.Compression,(long)EncoderValue.CompressionLZW);
myEncoderParameters.Param[0] = myEncoderParameter;
return myEncoderParameters;
}
It is also possible to get more then one encoder’s but why to do this is mostly a ? for me.