Without effecting Quality Reduce Image File Size While resizing Saving to Folder in ASP.Net

Last Reply on Aug 31, 2013 02:37 AM By Mudassar

Posted on Aug 31, 2013 02:26 AM

Sir,

I have uploaded a .jpg image and resized as Small and Large thumbnail. The small image is (135 X 173) but after resizing the small thumbnail image file size is of 40.8 KB. And mostly all images (around 64) thumbnails having file size more than 40 KB. Due to which thmubnails taking lot of time to get display.

Is there a way through which we can reduce the image file size upto 8 to 12 kb, but without effecting image quaity??

below is the source code that i am using for resizing an image:

 

 

If FileUpload1.HasFile Then
                Dim stream As Stream = FileUpload1.PostedFile.InputStream
                Dim x As String = IO.Path.GetExtension(FileUpload1.FileName).ToLower
                If x = ".jpg" Or x = ".jpeg" Then
                    Dim image As New Bitmap(stream)
                    Dim imgwidth As Integer
                    Dim imgheight As Integer
                    Dim picwidth As Integer
                    Dim picheight As Integer
                    Dim aspect As Double
                    Dim imgwidth1 As Integer
                    Dim imgheight1 As Integer
                    Dim picwidth1 As Integer
                    Dim picheight1 As Integer
                    Dim aspect1 As Double

                    picwidth = 135 'for small image
                    picheight = 300

                    picwidth1 = 500 'for big image
                    picheight1 = 500  

                    imgwidth = image.Width
                    imgheight = image.Height
                    imgwidth1 = image.Width
                    imgheight1 = image.Height
                    If imgwidth > picwidth Then
                        aspect = picwidth / imgwidth
                        imgwidth = imgwidth * aspect
                        imgheight = imgheight * aspect
                    End If
                    If imgheight > picheight Then
                        aspect = picheight / imgheight
                        imgwidth = imgwidth * aspect
                        imgheight = imgheight * aspect
                    End If
                    If imgwidth1 > picwidth1 Then
                        aspect1 = picwidth1 / imgwidth1
                        imgwidth1 = imgwidth1 * aspect1
                        imgheight1 = imgheight1 * aspect1
                    End If
                    If imgheight1 > picheight1 Then
                        aspect1 = picheight1 / imgheight1
                        imgwidth1 = imgwidth1 * aspect1
                        imgheight1 = imgheight1 * aspect1
                    End If

                    filename = DateTime.Now.Year & "-" & DateTime.Now.Month & "-" & DateTime.Now.Day & "-" & DateTime.Now.Hour & "-" & DateTime.Now.Minute & "-" & DateTime.Now.Second & "-" & DateTime.Now.Millisecond & x

                    '--Save small image
                    Dim target As New Bitmap(imgwidth, imgheight)
                    Dim graphic As Graphics = Graphics.FromImage(target)
                    graphic.DrawImage(image, 0, 0, imgwidth, imgheight)
                    target.Save(Server.MapPath("photos/small/" & filename))

                    '--Save large image
                    Dim target1 As New Bitmap(imgwidth1, imgheight1)
                    Dim graphic1 As Graphics = Graphics.FromImage(target1)
                    graphic1.DrawImage(image, 0, 0, imgwidth1, imgheight1)
                    target1.Save(Server.MapPath("photos/semi/" & filename))
             
                    'save to database
                    Dim con As New SqlConnection(constr)
                    Dim cmd As New SqlCommand("insert into ImageTable(Picture1) values(@Picture1)", con)
                    cmd.Parameters.AddWithValue("@Picture1", filename)
                    con.Open()
                    cmd.ExecuteNonQuery()
                    con.Close()			 
                End If
End If