You must Sign In to post a response.
  • Category: .NET

    How to compress image in vb.net.

    hello sir,
    i want t know that how to compress image in vb.net.Actually i want to store in database but the size of pic is very large e.g(4.5mb).
    I want to store image in compress format.
    How it is possible and i also want to show compressed image in picture box.
    I am using Opendialogbox().
  • #725333
    user can change selected compression level in own program. You can save an image with compression file format and also effect the compression and image quality. I refer some useful link below which help you in How to compress image in vb.net.
    [1]http://www.codeproject.com/Questions/596047/imagepluscompressionplususingplusvb-net
    [2] http://www.vb-helper.com/howto_net_optimize_jpg.html

  • #725346
    'Interpolation mode' class from System.Drawing.Graphics namespace will reoslve your problems
    check following code snippet for more details

    Private firstImg As System.Drawing.Image
    Private outputImg As System.Drawing.Image
    'here i am compressing image by 10%
    dim iCompressPer as integer= 10
    'resize the image by percent
    Dim integerX, integerY As Integer
    integerX = Int(imgSource.Width / 100 * iCompressPer)
    integerY = Int(imgSource.Height / 100 * iCompressPer)
    Dim objBitmap As Drawing.Bitmap = New System.Drawing.Bitmap(integerX, integerY)
    Dim objGraphics As System.Drawing.Graphics = Drawing.Graphics.FromImage(objBitmap)

    Select Case intType
    Case 0
    objGraphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.Default
    Case 1
    objGraphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
    Case 2
    objGraphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBilinear
    Case 3
    objGraphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
    End Select

    objGraphics.DrawImage(imgSource, 0, 0, integerX, integerY)
    outputImg = objBitmap



    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #725357
    Hi,

    try like below code to compress your image and save in location then you show in the picture box


    'create instance for image class
    Dim myimg As System.Drawing.Image = Nothing
    'get uploaded image input stream
    myimg = System.Drawing.Image.FromStream("your file input stream")
    'resize it using thumbnailimage method
    myimg = myimg.GetThumbnailImage(50, 100, Nothing, IntPtr.Zero)
    Dim str As New MemoryStream()
    'Save it in the server images folder
    myimg.Save("C:\compressed_img.jpg", myimg.RawFormat)

    Regards
    N.Ravindran
    Your Hard work never fails

  • #725393
    Hi,
    To save the image in database you may convert the image to into byte.
    As per my understanding it is not a good idea to save the image in database. If you start saving the images in database, DB size will grow and the performance will have direct impact. It is always good save the images in a folder and save the path in the database.

    If you are still looking for the code then please refer: shabdar.org/sql-server/105-store-save-images-in-sql-server.html


    Regards,
    Asheej T K


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.