Looking for component to resize image

Mondeo

Well-known member
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
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
 
System.Drawing

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

Code:
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? ;)
 
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.
[VB]
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)
[/VB]
 
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?
 
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 Im 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.
 
Back
Top