Completely Free .NET word API

Resources » Code Snippets » ASP.NET WebForms

How to compress the image

Updated: Category: ASP.NET WebForms
Author: Member Level: GoldPoints: 10

Hi, Here we have to discuss how to compress the image..

suppose if we upload the large image , may be every image size will be vary

so in order to avoid varying the image size , you have to Compress image before

saving to database.. this is the code follows

Imports System.IO
Imports System.Drawing
Partial Class Default2
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Const bmpW = 200
Const bmpH = 175
If (FileUpload1.HasFile) Then

Label1.Text = ""

If (CheckFileType(FileUpload1.FileName)) Then
Dim newWidth As Integer = bmpW
Dim newHeight As Integer = bmpH
Dim upName As String = Mid(FileUpload1.FileName, 1, (InStr(FileUpload1.FileName, ".") - 1))
' Dim upname As String = FileUpload1.FileName
Dim filePath As String = "~/Upload/" & upName & ".png"
Dim upBmp As Bitmap = Bitmap.FromStream(FileUpload1.PostedFile.InputStream)
Dim newBmp As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb)
newBmp.SetResolution(72, 72)
Dim upWidth As Integer = upBmp.Width
Dim upHeight As Integer = upBmp.Height
Dim newX As Integer = 0
Dim newY As Integer = 0
Dim reDuce As Decimal
If upWidth > upHeight Then
reDuce = newWidth / upWidth
newHeight = Int(upHeight * reDuce)
newY = Int((bmpH - newHeight) / 2)
newX = 0
ElseIf upWidth < upHeight Then
reDuce = newHeight / upHeight
newWidth = Int(upWidth * reDuce)
newX = Int((bmpW - newWidth) / 2)
newY = 0
ElseIf upWidth = upHeight Then
reDuce = newHeight / upHeight
newWidth = Int(upWidth * reDuce)
newX = Int((bmpW - newWidth) / 2)
newY = Int((bmpH - newHeight) / 2)
End If

Dim newGraphic As Graphics = Graphics.FromImage(newBmp)


newGraphic.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

newGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight)

newBmp.Save(MapPath(filePath), Imaging.ImageFormat.Png)

Image1.ImageUrl = filePath
Image1.Visible = True
Catch ex As Exception
Label1.Text = ex.ToString
End Try
Label1.Text = "Please select a picture with a file format extension of either Bmp, Jpg, Jpeg, Gif or Png."
End If
End If
End Sub
Function CheckFileType(ByVal fileName As String) As Boolean
Dim ext As String = Path.GetExtension(fileName)
Select Case ext.ToLower()
Case ".gif"
Return True
Case ".png"
Return True
Case ".jpg"
Return True
Case ".jpeg"
Return True
Case ".bmp"
Return True
Case Else
Return False
End Select
End Function
End Class

Did you like this resource? Share it with your friends and show your love!

Responses to "How to compress the image"
Author: marrisa  24 Jun 2013   Member Level: Bronze   Points : 4
thks for sharing. i've been engaged in image compression for a long time. now, get another good way.
also, i'd like to share my simpler method of image compression. just as the code below:
Dim Image As New RasterEdgeImaging()

Private Sub ImageCompression()
If True Then
ImageEncoder = ImageEncoder.Group3Encoding
End If
End Sub
Image.ImageCompression(@"C:\1.tif", "C:\2.tif", ImageEncoder ImageEncoder.Group3Encoding)
hope the image compression will be helpful. :)


Post Comment:

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India