Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Is anyone aware of a .net component that can

 

1. Take a jpeg of say 400x300

2. Return another new jpeg of smaller dimensions say 200 x 150

3. Most importantly the new file is lower resolution and hence a smaller file size

 

Thanks

Posted

System.Drawing

 

The classes in the System.Drawing namespace can facilitate this very easily.

 

'Imports
Imports System.Drawing
Imports System.Drawing.Imaging



'Code
Dim oldImg As Image
Dim newImg As Bitmap

'Load original image
oldImg = Image.FromFile(fileName)

'Create new image at new size
newImg = New Bitmap(oldImg, 200, 150)

'Save
newImg.Save(newFileName, ImageFormat.Jpeg)

 

Simple, huh? ;)

Never trouble another for what you can do for yourself.
  • Leaders
Posted

It becomes a little more complicated if you want to choose the quality that the jpeg will be saved with, which, of course, has a big impact of file size (and equally important, appearance). This is the code I used in one of my apps to specify the quality to save a jpeg with.

Dim Quality As Integer = 40 ' Medium/High quality
 
'Get a list of codecs
Dim Codecs As Imaging.ImageCodecInfo() = _
   Imaging.ImageCodecInfo.GetImageEncoders
Dim JpegCodec As Imaging.ImageCodecInfo

'Find the jpeg codec
For Each codec As Imaging.ImageCodecInfo In Codecs
   If codec.MimeType = "image/jpeg" Then
       JpegCodec = codec
   End If
Next
'If not found, throw an exception
If JpegCodec Is Nothing Then _
   Throw New Exception("Jpeg codec not found.");

'Use an EncoderParameters to specify quality
Dim Prams As New Imaging.EncoderParameters
Prams.Param(0) = New Imaging.EncoderParameter(Imaging.Encoder.Quality, Quality)

'Save Image
MyImage.Save(cdgSave.FileName, JpegCodec, Prams)

[sIGPIC]e[/sIGPIC]
Posted

Okay thanks guys.

 

Do you know what kind of performance overhead these classes have, are they heavy on server resources?

 

My application will be processing 32 images one after the other when I call my method. Is it likely to be slow do you think?

  • Leaders
Posted
That is all a matter of how often this will be happening. I think it will be perfectly acceptable for this operation to happen once and a while, but if you are looking at a busy server and this is a common function then it could slow things down. Maybe running the code on a low-priority thread could help prevent things slowing down, but I'm really not sure about that. The best way to find out, I would think, is to actually test it and see what the impact is.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...