.jpg compression with .net

  • Thread starter Thread starter RCIC
  • Start date Start date
R

RCIC

I want to upload a file to my website. I'd like to
compress the file on upload so when I redisplay the file
on a website it is as small possible. Is their an easy
way to compress jpgs in .Net?
 
You could take the file and read it into a bitmap object and use
yourbitmap.getthumbnailimage(intWidth,intHeight,nothing,nothing) , to put
the image into a thumbnail and resize it then use
the system.drawing.image save method ie yourbitmap.save("strfileandpath",
ImageFormat.Jpeg)
Something like this
dim myNewImage as system.drawing.image
mynewimage = mggetthumbnail(strFileLoc,intWidth,intHeight)
mynewimage.save(strSaveFileLoc,ImageFormat.Jpeg)

Function mgGetThumbNail(strFile as string, intWidth as integer, intHeight as
integer) as system.drawing.image
Dim myBitmap As bitmap = New Bitmap(strFile)
return mybitmap.getthumbnailimage(intWidth,intHeight,nothing,nothing)
End Function
 
I want to upload a file to my website. I'd like to
compress the file on upload so when I redisplay the file
on a website it is as small possible. Is their an easy
way to compress jpgs in .Net?

Compressing jpeg images doesn't make much sense. Try creating
zip/tgz/rar/ace archive from jpeg image - and you'll se what I'm talking
about.

regards
Bogdan
 
This is a code snippet from MSDN, which shows you how to set the compression
value when you save a JPG file.

private void SaveJPGWithCompressionSetting( Image image, string szFileName,
long lCompression )
{
EncoderParameters eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter( Encoder.Quality, lCompression );
ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
image.Save( szFileName, ici, eps );
}
 
D'oh! I forgot one of the helper functions for that.

private static 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;
}
 
Back
Top