Reduce image as byte and retrieve fast

Last Reply one day ago By kalpesh

Posted 7 days ago

I was trying to reduce the original image size and save it to db as byte. That way I was hoping the image would retrieve fast when the page loads. The code below reduces the original image to (Dim percentWidth As Single = CSng(350) / CSng(originalWidth)) and (Dim percentHeight As Single = CSng(350) / CSng(originalHeight)) as shown below. The issue is this size is not small enough and takes too much time to retrieve the image. Unfortunately, when I change the CSng(350) to smaller number the quality of the picture will be very bad. So my question is how do I save image as byte, as small as possible so it retrieves fast when the page loads? Here is the code I tried but takes a little too long to retrieve. Please keep in mind two things. The image should be small and save as byte and when the page loads it must retrieve the image fast (in 1 second) and without compromising quality.

Thank you for your time.

           Dim filePath As String = FileUpload1.PostedFile.FileName
           Dim filename As String = Path.GetFileName(filePath)
           Dim ext As String = Path.GetExtension(filename)
           Dim contenttype As String = String.Empty
           Select Case ext
               Case ".jpg"
                   contenttype = "image/jpg"
                   Exit Select
           End Select      
               If contenttype <> String.Empty Then
                   Dim fs As Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
                   Dim originalWidth As Integer = fs.Width
                   Dim originalHeight As Integer = fs.Height
                   Dim percentWidth As Single = CSng(350) / CSng(originalWidth)
                   Dim percentHeight As Single = CSng(350) / CSng(originalHeight)
                   Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
                   Dim newWidth As Integer = CInt((originalWidth * percent))
                   Dim newHeight As Integer = CInt((originalHeight * percent))
                   Dim newImage As Image = New Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb)
                   Using graphics As Graphics = graphics.FromImage(newImage)
                       graphics.CompositingQuality = CompositingQuality.HighQuality
                       graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
                       graphics.SmoothingMode = SmoothingMode.HighQuality
                       graphics.DrawImage(fs, 0, 0, newWidth, newHeight)
                   End Using
                   Dim bytes() As Byte
                   Dim ms As MemoryStream = New MemoryStream
                   Dim codec As ImageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(Function(c) c.FormatID = ImageFormat.Png.Guid)
                   Dim jpegParms As EncoderParameters = New EncoderParameters(1)
                   jpegParms.Param(0) = New EncoderParameter(Encoder.Quality, 95L)
                   newImage.Save(ms, codec, jpegParms)
                   bytes = ms.ToArray
                   comnd.Parameters.AddWithValue("@ContentType1", SqlDbType.VarChar).Value = contenttype
                   comnd.Parameters.AddWithValue("@Name1", SqlDbType.VarChar).Value = filename
                 comnd.Parameters.AddWithValue("@Photo1", SqlDbType.VarBinary).Value = bytes
                End If


You are viewing reply posted by: dharmendr 7 days ago.
I agree, here is the link: