Public Module mdl_Common
    Public MainForm As Form1 = Nothing

    Public Function GetLocalIPAddress() As String
        Dim IPAdress As System.Net.IPAddress
        Dim HostName As String = System.Net.Dns.GetHostName '获得本机的机器名
        IPAdress = System.Net.Dns.GetHostEntry(HostName).AddressList.GetValue(0) '获得本机的IP
        Return IPAdress.ToString '本机的IP
    End Function

    Public Function GetIPFromNetUrl(ByVal url As String) As String
        Dim objIPHostEntry As System.Net.IPHostEntry

        Try
            objIPHostEntry = System.Net.Dns.GetHostEntry(url) '把http://blog.sina.com.cn/u/1459845580的域名对应的ip地址解析出来
            Return objIPHostEntry.AddressList(0).ToString
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        Return ""
    End Function

    '验证域名是否合法`
    Function IsValidDomainUsingUri(domain As String) As Boolean
        Try
            ' 尝试将域名转换为URI
            ' 添加http://前缀使URI解析器能够正确识别
            Dim uriString As String = "http://" & domain

            Dim uri As New Uri(uriString)

            ' 检查主机名是否符合域名规则
            If String.IsNullOrWhiteSpace(uri.Host) Then
                Return False
            End If

            ' 验证主机名是否与传入的域名匹配
            Return uri.Host.Equals(domain, StringComparison.OrdinalIgnoreCase)
        Catch ex As Exception
            ' 如果URI解析失败，则不是有效域名
            Return False
        End Try
    End Function

    '对图片进行缩放
    '将图片Image缩放到aimRect指定的区域
    Public Function ZoomImage(ByRef image As Image, ByVal aimRect As Rectangle) As Image
        Dim factor As Double = aimRect.Width / image.Width
        Dim factor_y As Double = aimRect.Height / image.Height

        '按照最小的缩放比确定新的矩形框
        If factor > factor_y Then
            factor = factor_y
        End If

        Dim newRect As New Rectangle
        newRect.Width = factor * image.Width
        newRect.Height = factor * image.Height

        Dim new_Image As New Bitmap(newRect.Width, newRect.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
        Dim new_Graphic As Graphics = Graphics.FromImage(new_Image)
        new_Graphic.DrawImage(image, newRect, New Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel)
        Return new_Image
    End Function

    '并获取数据字节
    Public Function GetBytesFromImage(ByVal image As Image, ByRef picBuffer() As Byte) As Boolean
        Dim stream As New System.IO.MemoryStream(picBuffer)
        image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)
        Return True
    End Function

    Public Function GetImageFromBytes(ByRef picBuffer() As Byte) As Image
        Dim image As Image = Nothing
        Try
            Dim stream As New System.IO.MemoryStream(picBuffer)
            image = image.FromStream(stream)
        Catch ex As Exception
        End Try

        Return image
    End Function

    Public Function isDoubleValue(ByVal valString As String) As Boolean
        Dim m_dbl As Double = 0
        Try
            m_dbl = CDbl(valString)
            Return True
        Catch ex As Exception
        End Try

        Return False
    End Function
End Module
