Mondeo Posted November 14, 2006 Posted November 14, 2006 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 Quote
MrPaul Posted November 14, 2006 Posted November 14, 2006 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? ;) Quote Never trouble another for what you can do for yourself.
Leaders snarfblam Posted November 14, 2006 Leaders Posted November 14, 2006 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) Quote [sIGPIC]e[/sIGPIC]
Mondeo Posted November 15, 2006 Author Posted November 15, 2006 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? Quote
Leaders snarfblam Posted November 15, 2006 Leaders Posted November 15, 2006 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.