how to reduce image size in

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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
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)
                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)
            End If
    End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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.
kamal_2002Author Commented:
doest the code of compreesing the images consume resources like the cpu or the ram????
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.