locked
Image manipulation/resize/compress RRS feed

  • Question


  • Hello,

    In the version of our app which is for extern users (LS Browserapplication) the use can load images from a camera - the problem is that those images (JPG) can be very large...

    how to change the size of that pictures (jpg) so that they have a fixes size (9x13) and a lower resolution?

    robert

    Wednesday, December 5, 2012 3:15 PM

Answers

  • Hi madrianr,

    I create this class for me and shareit with comunity

    I created this class to reduce the size of images before saving them and use in all my projects, I will share with the community

    place this class in the server project

    Imports System.IO
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Drawing2D
    
    Public Class Image_Helper
    
        ''' <summary>
        ''' Retorna uma imagem redimensionada, ex:
        ''' de 100 à 500 KB = 60% do original;
        ''' de 500 à ... KB = 40% do original.
        ''' </summary>
        ''' <param name="Img">Imagem</param>
        ''' <returns>Image</returns>
        ''' <remarks></remarks>
        Public Shared Function GetImageByScale(Img As Image) As Image
            Dim Image As Image = Nothing
            Dim Size As Integer = 0
            Using Ms = New MemoryStream
                Img.Save(Ms, ImageFormat.Jpeg)
                Size = Ms.ToArray.Length / 1024
            End Using
            'de 100 a 500 KB
            If Size > 100 AndAlso Size < 500 Then
                Image = Image_Helper.GetImageByScale(Img, 60)
            End If
            'acima de 500 KB
            If Size > 500 Then
                Image = Image_Helper.GetImageByScale(Img, 40)
            End If
            'de 100 a 0
            If Size < 100 Then
                Image = Img
            End If
            Return Image
        End Function
    
        ''' <summary>
        ''' Retorna uma imagem redimensionada, ex:
        ''' de 100 à 500 KB = 60% do original;
        ''' de 500 à ... KB = 40% do original.
        ''' </summary>
        ''' <param name="Img">Imagem</param>
        ''' <returns>Byte()</returns>
        ''' <remarks></remarks>
        Public Shared Function GetImageByteArrayByScale(Img As Byte()) As Byte()
            Dim Image() As Byte = Nothing
            Dim Size As Integer = Img.Length / 1024
            'de 100 a 500 KB
            If Size > 100 AndAlso Size < 500 Then
                Image = Image_Helper.GetImageByteArrayByScale(Img, 60)
            End If
            'acima de 500 KB
            If Size > 500 Then
                Image = Image_Helper.GetImageByteArrayByScale(Img, 40)
            End If
            'de 100 a 0
            If Size < 100 Then
                Image = Img
            End If
            Return Image
        End Function
    
        ''' <summary>
        ''' Retorna uma imagem redimensionada
        ''' </summary>
        ''' <param name="Img">Imagem</param>
        ''' <param name="Percentual">Percentual de redimencionamento</param>
        ''' <returns>Image</returns>
        ''' <remarks></remarks>
        Public Shared Function GetImageByScale(Img As Image, Percentual As Integer) As Image
            'origem
            Dim OrigemLargura As Integer = Img.Width
            Dim OrigemAltura As Integer = Img.Height
            Dim OrigemX As Integer = 0
            Dim OrigemY As Integer = 0
            'destino
            Dim DestinoX As Integer = 0
            Dim DestinoY As Integer = 0
            Dim DestinoPercentual As Single = (CSng(Percentual) / 100)
            'Calcula a altura e largura da imagem redimensionada
            Dim DestinoLargura As Integer = CInt(Math.Truncate(OrigemLargura * DestinoPercentual))
            Dim DestinoAltura As Integer = CInt(Math.Truncate(OrigemAltura * DestinoPercentual))
            'Cria um novo objeto bitmap
            Dim BmImagem As New Bitmap(DestinoLargura, DestinoAltura, PixelFormat.Format24bppRgb)
            'Define a resolu~ção do bitmap.
            BmImagem.SetResolution(Img.HorizontalResolution, Img.VerticalResolution)
            'Crima um objeto graphics e defina a qualidade
            Dim GrImagem As Graphics = Graphics.FromImage(BmImagem)
            GrImagem.InterpolationMode = InterpolationMode.HighQualityBicubic
            'Desenha a imge usando o método DrawImage() da classe grafica
            GrImagem.DrawImage(Img, New Rectangle(DestinoX, DestinoY, DestinoLargura, DestinoAltura), New Rectangle(OrigemX, OrigemY, OrigemLargura, OrigemAltura), GraphicsUnit.Pixel)
            GrImagem.Dispose()
            'libera o objeto grafico
            Return BmImagem
        End Function
    
        ''' <summary>
        ''' Retorna uma imagem redimensionada
        ''' </summary>
        ''' <param name="Img">Imagem</param>
        ''' <param name="Percentual">Percentual de redimencionamento</param>
        ''' <returns>Image</returns>
        ''' <remarks></remarks>
        Public Shared Function GetImageByteArrayByScale(Img As Byte(), Percentual As Integer) As Byte()
            Using ms = New MemoryStream(Img)
                Using source As Image = Image.FromStream(ms, True, False)
                    Using target = New MemoryStream()
                        'origem
                        Dim OrigemLargura As Integer = source.Width
                        Dim OrigemAltura As Integer = source.Height
                        Dim OrigemX As Integer = 0
                        Dim OrigemY As Integer = 0
                        'destino
                        Dim DestinoX As Integer = 0
                        Dim DestinoY As Integer = 0
                        Dim DestinoPercentual As Single = (CSng(Percentual) / 100)
                        Dim DestinoLargura As Integer = CInt(Math.Truncate(OrigemLargura * DestinoPercentual))
                        Dim DestinoAltura As Integer = CInt(Math.Truncate(OrigemAltura * DestinoPercentual))
                        'Cria um novo objeto bitmap
                        Dim BmImagem As New Bitmap(DestinoLargura, DestinoAltura, PixelFormat.Format24bppRgb)
                        'Define a resolu~ção do bitmap.
                        BmImagem.SetResolution(source.HorizontalResolution, source.VerticalResolution)
                        'Crima um objeto graphics e defina a qualidade
                        Dim GrImagem As Graphics = Graphics.FromImage(BmImagem)
                        GrImagem.InterpolationMode = InterpolationMode.HighQualityBicubic
                        'Desenha a imge usando o método DrawImage() da classe grafica
                        GrImagem.DrawImage(source, New Rectangle(DestinoX, DestinoY, DestinoLargura, DestinoAltura), New Rectangle(OrigemX, OrigemY, OrigemLargura, OrigemAltura), GraphicsUnit.Pixel)
                        'libera o objeto grafico
                        GrImagem.Dispose()
                        'salvando a imagem para o stream no formato jpeg
                        BmImagem.Save(target, ImageFormat.Jpeg)
                        Return target.ToArray
                    End Using
                End Using
            End Using
        End Function
    
    End Class

    example of using

    Private Sub Empresas_Inserting(entity As Empresa)
    	If entity.LogoMarca IsNot Nothing Then
    		entity.LogoMarca = Image_Helper.GetImageByteArrayByScale(entity.LogoMarca)
    	End If
    End Sub
    
    Private Sub Empresas_Updating(entity As Empresa)
    	If entity.LogoMarca IsNot Nothing Then
    		entity.LogoMarca = Image_Helper.GetImageByteArrayByScale(entity.LogoMarca)
    	End If
    End Sub



    • Edited by Marden LR Thursday, December 6, 2012 12:37 AM
    • Proposed as answer by Yann DuranModerator Thursday, December 6, 2012 2:38 AM
    • Marked as answer by Mad Rian Thursday, December 6, 2012 7:21 AM
    • Unmarked as answer by Mad Rian Thursday, December 6, 2012 12:26 PM
    • Marked as answer by Mad Rian Thursday, December 6, 2012 12:27 PM
    Thursday, December 6, 2012 12:32 AM

All replies

  • Hi madrianr,

    I create this class for me and shareit with comunity

    I created this class to reduce the size of images before saving them and use in all my projects, I will share with the community

    place this class in the server project

    Imports System.IO
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Drawing2D
    
    Public Class Image_Helper
    
        ''' <summary>
        ''' Retorna uma imagem redimensionada, ex:
        ''' de 100 à 500 KB = 60% do original;
        ''' de 500 à ... KB = 40% do original.
        ''' </summary>
        ''' <param name="Img">Imagem</param>
        ''' <returns>Image</returns>
        ''' <remarks></remarks>
        Public Shared Function GetImageByScale(Img As Image) As Image
            Dim Image As Image = Nothing
            Dim Size As Integer = 0
            Using Ms = New MemoryStream
                Img.Save(Ms, ImageFormat.Jpeg)
                Size = Ms.ToArray.Length / 1024
            End Using
            'de 100 a 500 KB
            If Size > 100 AndAlso Size < 500 Then
                Image = Image_Helper.GetImageByScale(Img, 60)
            End If
            'acima de 500 KB
            If Size > 500 Then
                Image = Image_Helper.GetImageByScale(Img, 40)
            End If
            'de 100 a 0
            If Size < 100 Then
                Image = Img
            End If
            Return Image
        End Function
    
        ''' <summary>
        ''' Retorna uma imagem redimensionada, ex:
        ''' de 100 à 500 KB = 60% do original;
        ''' de 500 à ... KB = 40% do original.
        ''' </summary>
        ''' <param name="Img">Imagem</param>
        ''' <returns>Byte()</returns>
        ''' <remarks></remarks>
        Public Shared Function GetImageByteArrayByScale(Img As Byte()) As Byte()
            Dim Image() As Byte = Nothing
            Dim Size As Integer = Img.Length / 1024
            'de 100 a 500 KB
            If Size > 100 AndAlso Size < 500 Then
                Image = Image_Helper.GetImageByteArrayByScale(Img, 60)
            End If
            'acima de 500 KB
            If Size > 500 Then
                Image = Image_Helper.GetImageByteArrayByScale(Img, 40)
            End If
            'de 100 a 0
            If Size < 100 Then
                Image = Img
            End If
            Return Image
        End Function
    
        ''' <summary>
        ''' Retorna uma imagem redimensionada
        ''' </summary>
        ''' <param name="Img">Imagem</param>
        ''' <param name="Percentual">Percentual de redimencionamento</param>
        ''' <returns>Image</returns>
        ''' <remarks></remarks>
        Public Shared Function GetImageByScale(Img As Image, Percentual As Integer) As Image
            'origem
            Dim OrigemLargura As Integer = Img.Width
            Dim OrigemAltura As Integer = Img.Height
            Dim OrigemX As Integer = 0
            Dim OrigemY As Integer = 0
            'destino
            Dim DestinoX As Integer = 0
            Dim DestinoY As Integer = 0
            Dim DestinoPercentual As Single = (CSng(Percentual) / 100)
            'Calcula a altura e largura da imagem redimensionada
            Dim DestinoLargura As Integer = CInt(Math.Truncate(OrigemLargura * DestinoPercentual))
            Dim DestinoAltura As Integer = CInt(Math.Truncate(OrigemAltura * DestinoPercentual))
            'Cria um novo objeto bitmap
            Dim BmImagem As New Bitmap(DestinoLargura, DestinoAltura, PixelFormat.Format24bppRgb)
            'Define a resolu~ção do bitmap.
            BmImagem.SetResolution(Img.HorizontalResolution, Img.VerticalResolution)
            'Crima um objeto graphics e defina a qualidade
            Dim GrImagem As Graphics = Graphics.FromImage(BmImagem)
            GrImagem.InterpolationMode = InterpolationMode.HighQualityBicubic
            'Desenha a imge usando o método DrawImage() da classe grafica
            GrImagem.DrawImage(Img, New Rectangle(DestinoX, DestinoY, DestinoLargura, DestinoAltura), New Rectangle(OrigemX, OrigemY, OrigemLargura, OrigemAltura), GraphicsUnit.Pixel)
            GrImagem.Dispose()
            'libera o objeto grafico
            Return BmImagem
        End Function
    
        ''' <summary>
        ''' Retorna uma imagem redimensionada
        ''' </summary>
        ''' <param name="Img">Imagem</param>
        ''' <param name="Percentual">Percentual de redimencionamento</param>
        ''' <returns>Image</returns>
        ''' <remarks></remarks>
        Public Shared Function GetImageByteArrayByScale(Img As Byte(), Percentual As Integer) As Byte()
            Using ms = New MemoryStream(Img)
                Using source As Image = Image.FromStream(ms, True, False)
                    Using target = New MemoryStream()
                        'origem
                        Dim OrigemLargura As Integer = source.Width
                        Dim OrigemAltura As Integer = source.Height
                        Dim OrigemX As Integer = 0
                        Dim OrigemY As Integer = 0
                        'destino
                        Dim DestinoX As Integer = 0
                        Dim DestinoY As Integer = 0
                        Dim DestinoPercentual As Single = (CSng(Percentual) / 100)
                        Dim DestinoLargura As Integer = CInt(Math.Truncate(OrigemLargura * DestinoPercentual))
                        Dim DestinoAltura As Integer = CInt(Math.Truncate(OrigemAltura * DestinoPercentual))
                        'Cria um novo objeto bitmap
                        Dim BmImagem As New Bitmap(DestinoLargura, DestinoAltura, PixelFormat.Format24bppRgb)
                        'Define a resolu~ção do bitmap.
                        BmImagem.SetResolution(source.HorizontalResolution, source.VerticalResolution)
                        'Crima um objeto graphics e defina a qualidade
                        Dim GrImagem As Graphics = Graphics.FromImage(BmImagem)
                        GrImagem.InterpolationMode = InterpolationMode.HighQualityBicubic
                        'Desenha a imge usando o método DrawImage() da classe grafica
                        GrImagem.DrawImage(source, New Rectangle(DestinoX, DestinoY, DestinoLargura, DestinoAltura), New Rectangle(OrigemX, OrigemY, OrigemLargura, OrigemAltura), GraphicsUnit.Pixel)
                        'libera o objeto grafico
                        GrImagem.Dispose()
                        'salvando a imagem para o stream no formato jpeg
                        BmImagem.Save(target, ImageFormat.Jpeg)
                        Return target.ToArray
                    End Using
                End Using
            End Using
        End Function
    
    End Class

    example of using

    Private Sub Empresas_Inserting(entity As Empresa)
    	If entity.LogoMarca IsNot Nothing Then
    		entity.LogoMarca = Image_Helper.GetImageByteArrayByScale(entity.LogoMarca)
    	End If
    End Sub
    
    Private Sub Empresas_Updating(entity As Empresa)
    	If entity.LogoMarca IsNot Nothing Then
    		entity.LogoMarca = Image_Helper.GetImageByteArrayByScale(entity.LogoMarca)
    	End If
    End Sub



    • Edited by Marden LR Thursday, December 6, 2012 12:37 AM
    • Proposed as answer by Yann DuranModerator Thursday, December 6, 2012 2:38 AM
    • Marked as answer by Mad Rian Thursday, December 6, 2012 7:21 AM
    • Unmarked as answer by Mad Rian Thursday, December 6, 2012 12:26 PM
    • Marked as answer by Mad Rian Thursday, December 6, 2012 12:27 PM
    Thursday, December 6, 2012 12:32 AM
  • Works great - thank's for sharing with us!

    robert

    Thursday, December 6, 2012 1:20 PM
  • @ Marden LR

    Nice. Thank you for share this.

    Regards


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Thursday, December 6, 2012 1:38 PM
  • Hi madrianr and babloo1436

    You're welcome. I'm just giving back a little of the knowledge I have gained here in this forum, glad to have helped people solve their problems :)

    Thursday, December 6, 2012 10:06 PM