Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Namespace PCSolution.Images

    Public Class Images
        Inherits System.Web.UI.Page

#Region "  ULOZIT FOTOGRAFII  "

        ''' <summary>
        ''' Uloží fotografii se zadanými rozměry
        ''' </summary>
        ''' <param name="Path">Cesta k uloženému souboru</param>
        ''' <param name="PathSave">Nová cesta kam se má soubor uložit</param>
        ''' <param name="OriginalFile">Název originálního souboru</param>
        ''' <param name="SaveFileName">Název souboru k uložení</param>
        ''' <param name="iWidth">Šířka</param>
        ''' <param name="iHeight">Výška</param>
        ''' <param name="lKvalita">Kvalita 0-100</param>
        ''' 
        Sub SaveFoto(ByVal Path As String, ByVal PathSave As String, _
            ByVal OriginalFile As String, ByVal SaveFileName As String, _
            ByVal iWidth As Integer, ByVal iHeight As Integer, ByVal lKvalita As Integer, Optional sExt As String = "jpg")

            Path = Path.Replace("\\", "\")
            sExt = sExt.Replace(".", "")
            sExt = sExt.ToLower
			OriginalFile = OriginalFile.ToLower

			Directory.CreateDirectory(PathSave)

            '## Upravit soubor proporcielne na vysku

            If iWidth = 0 Then

                Dim dblProcentoX As Double
                Dim dblProcentoY As Double
                Dim dblKolikProcent As Double
                Dim dblKolikProcentNavic As Double
                Dim intSirkaOrig As Integer
                Dim intVyskaOrig As Integer
                Dim rozmerX As Integer
                Dim objBMP As New Bitmap(Path & "\" & OriginalFile)

                intSirkaOrig = objBMP.Width
                intVyskaOrig = objBMP.Height

                If intVyskaOrig < iHeight Then iHeight = intVyskaOrig

                dblProcentoY = intVyskaOrig / 100
                dblKolikProcent = (100 - (iHeight / dblProcentoY))
                dblProcentoX = intSirkaOrig / 100
                dblKolikProcentNavic = dblKolikProcent * dblProcentoX
                rozmerX = intSirkaOrig - dblKolikProcentNavic
                objBMP.Dispose()

                Dim objBitmap As New Bitmap(Path & "\" & OriginalFile) ' Vytvorit nahled
                Dim objGraphics As Graphics
                Dim velikost As New Size(rozmerX, iHeight)
                objGraphics = Graphics.FromImage(objBitmap)

                Dim objNovaBitmapa As New Bitmap(objBitmap, velikost)
                'Nastaveni komprese
                Dim encoderParams As System.Drawing.Imaging.EncoderParameters = _
                    New System.Drawing.Imaging.EncoderParameters()

                Dim encoderParam As System.Drawing.Imaging.EncoderParameter = _
                    New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lKvalita)
                encoderParams.Param(0) = encoderParam

                Dim arrayICI As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
                Dim jpegICI As ImageCodecInfo
                jpegICI = arrayICI(0)

                Dim x As Integer
                For x = 0 To arrayICI.Length - 1

                    If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
                        jpegICI = arrayICI(x)
                        Exit For
                    End If

                Next
                'End Nastaveni komprese

                objBitmap.Dispose()
                objGraphics.Dispose()

                If sExt = "jpg" Then
                    objNovaBitmapa.Save(PathSave & "\" & SaveFileName & ".jpg", jpegICI, encoderParams)
                ElseIf sExt = "png" Then
                    objNovaBitmapa.Save(PathSave & "\" & SaveFileName & ".png", ImageFormat.Png)
                End If

                objNovaBitmapa.Dispose()

            End If

            '##

            '## Upravit soubor proporcielne na sirku

            If iHeight = 0 Then

                Dim dblProcentoX As Double
                Dim dblProcentoY As Double
                Dim dblKolikProcent As Double
                Dim dblKolikProcentNavic As Double
                Dim intSirkaOrig As Integer
                Dim intVyskaOrig As Integer
                Dim rozmerY As Integer
                Dim b As String = Path & "\" & OriginalFile
                Dim objBMP As New Bitmap(b)

                Dim objBMP2 As New Bitmap(Path & "\" & OriginalFile)
                intSirkaOrig = objBMP2.Width
                intVyskaOrig = objBMP2.Height

                If intSirkaOrig < iWidth Then iWidth = intSirkaOrig

                dblProcentoX = intSirkaOrig / 100
                dblKolikProcent = (100 - (iWidth / dblProcentoX))
                dblProcentoY = intVyskaOrig / 100
                dblKolikProcentNavic = dblKolikProcent * dblProcentoY
                rozmerY = intVyskaOrig - dblKolikProcentNavic
                objBMP2.Dispose()

                Dim objBitmap2 As New Bitmap(Path & "\" & OriginalFile) ' Vytvorit nahled
                Dim objGraphics2 As Graphics
                Dim velikost2 As New Size(iWidth, rozmerY)
                objGraphics2 = Graphics.FromImage(objBitmap2)

                Dim objNovaBitmapa2 As New Bitmap(objBitmap2, velikost2)
                'Nastaveni komprese
                Dim encoderParams2 As System.Drawing.Imaging.EncoderParameters = _
                    New System.Drawing.Imaging.EncoderParameters()

                Dim encoderParam2 As System.Drawing.Imaging.EncoderParameter = _
                    New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lKvalita)
                encoderParams2.Param(0) = encoderParam2

                Dim x As Integer
                Dim arrayICI2 As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
                Dim jpegICI2 As ImageCodecInfo
                jpegICI2 = arrayICI2(0)

                For x = 0 To arrayICI2.Length - 1

                    If (arrayICI2(x).FormatDescription.Equals("JPEG")) Then
                        jpegICI2 = arrayICI2(x)
                        Exit For
                    End If

                Next

                objBitmap2.Dispose()
                objGraphics2.Dispose()
                objBMP.Dispose()

                'End Nastaveni komprese
                If sExt = "jpg" Then
                    objNovaBitmapa2.Save(PathSave & "\" & SaveFileName & ".jpg", jpegICI2, encoderParams2)
                ElseIf sExt = "png" Then
                    IO.File.Delete(PathSave & "\" & SaveFileName & ".png")
                    objNovaBitmapa2.Save(PathSave & "\" & SaveFileName & ".png", ImageFormat.Png)
                End If

                objNovaBitmapa2.Dispose()


            End If

            If iWidth > 0 And iHeight > 0 Then

                Dim sBMP As String = Path & "\" & OriginalFile
                Dim objBMP As New Bitmap(sBMP)
                objBMP.Dispose()

                Dim objBitmap As New Bitmap(Path & "\" & OriginalFile) ' Vytvorit nahled
                Dim objGraphics As Graphics
                Dim velikost As New Size(iWidth, iHeight)
                objGraphics = Graphics.FromImage(objBitmap)

                Dim objNovaBitmapa As New Bitmap(objBitmap, velikost)
                'Nastaveni komprese
                Dim encoderParams As System.Drawing.Imaging.EncoderParameters = _
                    New System.Drawing.Imaging.EncoderParameters()

                Dim encoderParam As System.Drawing.Imaging.EncoderParameter = _
                    New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lKvalita)
                encoderParams.Param(0) = encoderParam

                Dim arrayICI As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
                Dim jpegICI As ImageCodecInfo
                jpegICI = arrayICI(0)

                Dim x As Integer
                For x = 0 To arrayICI.Length - 1

                    If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
                        jpegICI = arrayICI(x)
                        Exit For
                    End If

                Next
                'End Nastaveni komprese

                objBitmap.Dispose()
                objGraphics.Dispose()

                If sExt = "jpg" Then
                    objNovaBitmapa.Save(PathSave & "\" & SaveFileName & ".jpg", jpegICI, encoderParams)
                ElseIf sExt = "png" Then
                    objNovaBitmapa.Save(PathSave & "\" & SaveFileName & ".png", ImageFormat.Png)
                End If

                objNovaBitmapa.Dispose()

            End If

        End Sub

#End Region

#Region "  ORIZNOUT FOTOGRAFII  "

        Sub CropImage(ByVal Path As String, ByVal PathSave As String, _
                      ByVal OriginalFile As String, ByVal SaveFileName As String, _
                      ByVal iTop As Int16, ByVal iLeft As Int16, ByVal iRight As Int16, ByVal iBottom As Int16, Optional sExt As String = "jpg")

            Path = Path.Replace("\\", "\")
            sExt = sExt.Replace(".", "")
            sExt = sExt.ToLower
            OriginalFile = OriginalFile.ToLower

            Dim TopLeft As New Point(iTop, iLeft)
            Dim BottomRight As New Point(iBottom, iRight)

            Dim objBMP As New Bitmap(Path & "\" & OriginalFile)


            Dim btmCropped As New Bitmap((BottomRight.Y - TopLeft.Y), (BottomRight.X - TopLeft.X))
            Dim grpOriginal As Graphics = Graphics.FromImage(btmCropped)

            grpOriginal.DrawImage(objBMP, New Rectangle(0, 0, btmCropped.Width, btmCropped.Height), TopLeft.X, TopLeft.Y, btmCropped.Width, btmCropped.Height, GraphicsUnit.Pixel)

            If sExt = "jpg" Then

                'Nastaveni komprese
                Dim encoderParams As System.Drawing.Imaging.EncoderParameters = New System.Drawing.Imaging.EncoderParameters()

                Dim encoderParam As System.Drawing.Imaging.EncoderParameter = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80)
                encoderParams.Param(0) = encoderParam

                Dim arrayICI As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
                Dim jpegICI As ImageCodecInfo
                jpegICI = arrayICI(0)

                Dim x As Integer
                For x = 0 To arrayICI.Length - 1

                    If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
                        jpegICI = arrayICI(x)
                        Exit For
                    End If

                Next
                'End Nastaveni komprese

                Dim s As String = PathSave & "\" & SaveFileName & "." & sExt
                btmCropped.Save(s, jpegICI, encoderParams)

            ElseIf sExt = "png" Then

                Dim s As String = PathSave & "\" & SaveFileName & "." & sExt
                btmCropped.Save(s, ImageFormat.Png)

            End If

            objBMP.Dispose()
            btmCropped.Dispose()
            grpOriginal.Dispose()

            Try
                System.IO.File.Delete(PathSave & "\temp." & sExt)
            Catch ex As Exception
            End Try

        End Sub

#End Region


	End Class

End Namespace