Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Drawing


Public Module CommonData

    Dim sqlCon As SqlConnection
    Dim sqlCmd As SqlCommand
    Dim ds As Data.DataSet
    Dim dt As Data.DataTable
    Dim dr As SqlDataReader
    Dim sqlda As SqlDataAdapter

    Public Function getTable(ByVal strSPName As String, ByVal strTableName As String) As Data.DataTable
        getDBObjects(strSPName)
        sqlda.Fill(ds, strTableName.Trim)
        dt = ds.Tables(strTableName.Trim)
        ReleaseObjects()
        Return dt
    End Function

    Public Function Generate_Code(ByVal strqry As String, ByVal strformat As String) As String
        Dim i As String
        Dim qry As String
        Dim code As String = ""
        i = "1"
        i = Format(Val(i), "000")
        While True
            code = strformat.Trim & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
            qry = strqry.Trim & code & "'"
            If IsAlreadyPresent(qry) Then
                i = Val(i) + 1
                i = Format(Val(i), "000")
            Else
                Return code
                Exit Function
            End If
        End While
        Return code
    End Function

    Public Function ExecQuery(ByVal strSPName As String, ByVal sqlparamcoll As SqlParameterCollection) As String
        Dim p As SqlParameter
        getDBObjects("")
        With sqlCmd
            .CommandText = strSPName.Trim
            .CommandType = Data.CommandType.StoredProcedure
            .Connection = sqlCon
            .Parameters.Clear()
            For Each p In sqlparamcoll
                Dim par As New SqlParameter()
                With par
                    .ParameterName = p.ParameterName
                    .SqlDbType = p.SqlDbType
                    .Size = p.Size
                    .Value = p.Value
                End With
                .Parameters.Add(par)
            Next
        End With
        Try
            sqlCon.Open()
            sqlCmd.ExecuteNonQuery()
            sqlCon.Close()
            ReleaseObjects()
            Return True
        Catch ex As Exception
            sqlCon.Close()
            ReleaseObjects()
            Return ex.Message.ToString
        End Try
    End Function
    Public Function getImageName(ByVal strPath As String) As String
        Dim str As String = strPath.Substring(strPath.LastIndexOf("\") + 1)
        Return str
    End Function

    Sub KeepAspectRatio(ByRef Image1 As System.Web.UI.WebControls.Image, ByVal FilePath As String, ByVal width As Long, ByVal height As Long)
        Dim x As Long
        Dim y As Long
        Dim px As Long
        Dim py As Long
        Dim dx As Long
        Dim dy As Long
        Dim nx As Long
        Dim ny As Long
        Dim nPercent As Single = 0
        Dim img As Image = Nothing
        img = System.Drawing.Image.FromFile(FilePath)
        px = img.Height
        py = img.Width
        x = height
        y = width
        nPercent = (CSng(y) / CSng(py))

        Image1.Width = CInt((py * nPercent))
        Image1.Height = CInt((px * nPercent))
        'If px > x And py > y Then
        '    dx = px - x
        '    dy = py - y
        '    nx = x + dx
        '    ny = (y * (x + dx)) / x
        '    If ny <= py Then
        '        Image1.Width = ny
        '        Image1.Height = nx
        '    Else
        '        ny = y + dy
        '        nx = (x * (y + dy)) / y
        '        Image1.Width = ny
        '        Image1.Height = nx
        '    End If
        'End If
        'If px < x And py < y Then
        '    dx = x - px
        '    dy = y - py
        '    nx = x - dx
        '    ny = (y * (x - dx)) / x
        '    If ny >= py Then
        '        Image1.Width = ny
        '        Image1.Height = nx
        '    Else
        '        ny = y - dy
        '        nx = (x * (y - dy)) / y
        '        Image1.Width = ny
        '        Image1.Height = nx
        '    End If
        'End If
    End Sub
    Sub KeepAspectRatioNew1(ByRef Image1 As System.Web.UI.WebControls.Image, ByVal FilePath As String, ByVal newWidth As Long, ByVal newHeight As Long)
        Dim x As Long
        Dim y As Long
        Dim px As Long
        Dim py As Long
        Dim dx As Long
        Dim dy As Long
        Dim nx As Long
        Dim ny As Long
        Dim img As Image = Nothing
        img = System.Drawing.Image.FromFile(FilePath)
        x = img.Height
        y = img.Width
        px = Image1.Height.Value
        py = Image1.Width.Value
        If px > x And (py > y Or py = y) Then
            dx = px - x
            dy = py - y
            nx = x + dx
            ny = (y * (x + dx)) / x
            If ny <= py Then
                Image1.Width = ny
                Image1.Height = nx
            Else
                ny = y + dy
                nx = (x * (y + dy)) / y
                Image1.Width = ny
                Image1.Height = nx
            End If
        End If
        If px < x And (py < y Or py = y) Then
            dx = x - px
            dy = y - py
            nx = x - dx
            ny = (y * (x - dx)) / x
            If ny >= py Then
                Image1.Width = ny
                Image1.Height = nx
            Else
                ny = y - dy
                nx = (x * (y - dy)) / y
                Image1.Width = ny
                Image1.Height = nx
            End If
        End If
        x = newHeight
        y = newWidth
        px = Image1.Height.Value
        py = Image1.Width.Value
        If x > nx Or ny = y Then
            nx = x
            ny = (py * x) / px
            Image1.Height = nx
            Image1.Width = ny
        Else
            ny = y
            nx = (px * y) / py
            Image1.Height = nx
            Image1.Width = ny
        End If
    End Sub
    Private Sub getDBObjects(ByVal strQuery As String)
        sqlCon = New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        sqlCmd = New SqlCommand()
        ds = New Data.DataSet()
        If Not strQuery.Trim.Length = 0 Then
            sqlda = New SqlDataAdapter(strQuery, sqlCon)
        End If
    End Sub

    Private Sub ReleaseObjects()
        sqlCon = Nothing
        sqlCmd = Nothing
        ds = Nothing
        sqlda = Nothing
    End Sub

    Sub KeepAspectRatioNew(ByRef Image1 As System.Web.UI.WebControls.Image, ByVal FilePath As String, ByVal newWidth As Long, ByVal newHeight As Long)
        Dim x As Long
        Dim y As Long
        Dim nx As Long
        Dim ny As Long
        Dim img As Image = Nothing
        If FilePath = "" Or IsDBNull(FilePath) Then
            Image1.Visible = False
            Exit Sub
        End If
        If System.IO.File.Exists(FilePath) = False Then
            Image1.Visible = False
            Exit Sub
        End If
        img = System.Drawing.Image.FromFile(FilePath)
        Dim nPercent As Single = 0
        x = img.Height
        y = img.Width
        nx = newHeight
        ny = newWidth
       
       
        If (y = ny Or y > ny) Then
            '========13.01.09
            'ny = (y * nx) / x
            'If (ny = newWidth Or ny < newWidth) Then
            '    Image1.Height = nx
            '    Image1.Width = ny
            'Else
            '    x = nx
            '    y = ny
            '    ny = newWidth
            '    nx = (x * ny) / y
            '    Image1.Height = nx
            '    Image1.Width = ny
            'End If
            nPercent = (CSng(ny) / CSng(y))
            Image1.Width = CInt((y * nPercent))
            Image1.Height = CInt((x * nPercent))
            x = Image1.Height.Value
            y = Image1.Width.Value
            If (x = nx Or x > nx) Then
                nPercent = (CSng(nx) / CSng(x))
                Image1.Width = CInt((y * nPercent))
                Image1.Height = CInt((x * nPercent))
            End If

        ElseIf (x = nx Or x > nx) Then
            nPercent = (CSng(nx) / CSng(x))
            Image1.Width = CInt((y * nPercent))
            Image1.Height = CInt((x * nPercent))
            '========13.01.09
            'nx = (x * ny) / y
            'If (nx = newHeight Or nx < newHeight) Then
            '    Image1.Height = nx
            '    Image1.Width = ny
            'Else
            '    x = nx
            '    y = ny
            '    nx = newHeight
            '    ny = (y * nx) / x
            '    Image1.Height = nx
            '    Image1.Width = ny
            'End If
        ElseIf (y = ny Or y < ny) Then               '========13.01.09
            nPercent = (CSng(ny) / CSng(y))
            Image1.Width = CInt((y * nPercent))
            Image1.Height = CInt((x * nPercent))
            x = Image1.Height.Value
            y = Image1.Width.Value
            If (x = nx Or x > nx) Then
                nPercent = (CSng(nx) / CSng(x))
                Image1.Width = CInt((y * nPercent))
                Image1.Height = CInt((x * nPercent))
            End If

        ElseIf (x = nx Or x < nx) Then
            nPercent = (CSng(nx) / CSng(x))
            Image1.Width = CInt((y * nPercent))
            Image1.Height = CInt((x * nPercent))
        Else
            Image1.Width = y
            Image1.Height = x
        End If
    End Sub


   
End Module
