how to reduce image size in asp.net

hi all
i have a website and im retreiveing images from sql 2005 and shows them in my site
is their any way that i reduce the image size before showing it on my web

let say
1)get image from DB
2)Execute some code to reduce the size
30show image
thanks
kamal_2002Asked:
Who is Participating?
 
Tom BeckConnect With a Mentor Commented:
Here's some code I have been using to reduce the size of images. It will handle step 2.
Private Sub ResampleImages()
        Dim imgDir As String = "images/"
        Dim finalW As Integer = 640
        Dim finalH As Integer = 640
        Dim curFile As String
        Dim oldW, oldH As Integer
        Dim Info As System.Drawing.Imaging.ImageCodecInfo() = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders
        Dim Params As System.Drawing.Imaging.EncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
        Params.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80)
        For Each curFile In System.IO.Directory.GetFiles(imgDir, "*.jpg")
            Dim curImg As System.Drawing.Image = System.Drawing.Image.FromFile(curFile)
            oldW = curImg.Width
            oldH = curImg.Height
            If oldW > finalW And oldH > finalH Then
                Dim inp As New IntPtr()
                Dim imgCrpd As New Bitmap(curImg, finalW, finalH)
                Dim myGraphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(imgCrpd)
                myGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                myGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                myGraphic.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
                myGraphic.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
                myGraphic.DrawImage(curImg, 0, 0, finalW, finalH)
                curImg.Dispose()
                Info = ImageCodecInfo.GetImageEncoders
                Dim encoderParameters As EncoderParameters
                encoderParameters = New EncoderParameters(1)
                encoderParameters.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80)
                imgCrpd.Save(imgDir + System.IO.Path.GetFileName(curFile), Info(1), encoderParameters)
                imgCrpd.Dispose()
            End If
            curImg.Dispose()
        Next
    End Sub

Open in new window

0
 
Tom BeckCommented:
Note that the code I submitted assumes that the ratio of width to height in the images you are retrieving is consistant and that all are oriented on way, such as all horizontal rectangles or all vertical or all square. Otherwise it will distort any that are inconsistant as it is writen. You could add some means of calculating the ratio and orientation first and the adjusting the finalW and finalH before doing the resample so it works with all images.
0
 
kamal_2002Author Commented:
is this code to reduce the size like

80 * 80 to be for example 40*40

or it will reduce the actual size(weight) of the picture
for me i want to reduce the size for example from 80 kb to 30 kb
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Tom BeckCommented:
Reducing the width and height will reduce the file size exponentially, for example going from 80 x 80 to 40 x 40 reduces the number of pixels to 1/4 of the original, not 1/2 as one might assume. The code also adds an 80% of original jpeg compression (System.Drawing.Imaging.Encoder.Quality, 80) for further file size reduction.
0
 
masterpassCommented:
0
 
kamal_2002Author Commented:
doest the code of compreesing the images consume resources like the cpu or the ram????
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.