Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » Code Snippets » ASP.NET WebForms

How to compress the image


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Gold    Points: 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)
Try

newGraphic.Clear(Color.White)

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
Finally
upBmp.Dispose()
newBmp.Dispose()
newGraphic.Dispose()
End Try
Else
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"

No responses found. Be the first to respond...

Feedbacks      

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.


    Next Resource: Open a popup window
    Previous Resource: Viewstate Chunking
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Image Compression  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.