[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

Bitmap problems. Compression.

kcitrenbaum@gmail.com

1/20/2005 7:05:00 PM

I'm getting really sick of dealing with Bitmaps in dotNet. If I create
a Bitmap from a MemoryStream, I have to keep the stream around for as
long as I want to use the Bitmap. Is there any more efficient way of
using Bitmaps? Here's the situation. I want to compress a Jpeg image.
Here's the code:
private Bitmap CompressImage(Bitmap orig, int quality) {
Bitmap rtn= null;
if(quality == 100){return orig;}
if (orig == null) {
throw new Exception("No Image to compress!");
}
using (MemoryStream mems = new MemoryStream()) {
Bitmap b = null;
using(Bitmap t = new Bitmap(orig)){
EncoderParameters myEncoderParameters = new EncoderParameters(2);
EncoderParameter myEncoderParameter = new
EncoderParameter(Encoder.Compression, (long)
EncoderValue.CompressionLZW);
EncoderParameter myEncoderParameter1 = new
EncoderParameter(Encoder.Quality, (long) quality);
myEncoderParameters.Param[0] = myEncoderParameter;
myEncoderParameters.Param[1] = myEncoderParameter1;
t.Save(mems, GetEncoderInfo("image/jpeg"), myEncoderParameters);
b = new Bitmap(mems);
}
rtn = new Bitmap(b);
b.Dispose();
}
return rtn;
}

This works, but seems really inefficient. I have to keep creating
extra Bitmaps (t and rtn specificly) or else I get "Unspecified GDI+
Error" problems. Is there a better way?