2020年6月10日 星期三

vb.net 圖片互轉Byte [Image to Byte]


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    Public Function ImageToByte(ByVal img As Image, ByRef byteArray() As Byte) As Boolean
        Dim imgStream As MemoryStream = New MemoryStream()

        Try
            img.Save(imgStream, System.Drawing.Imaging.ImageFormat.Bmp)

            imgStream.Close()
            byteArray = imgStream.ToArray()
            imgStream.Dispose()
        Catch ex As Exception
            Return False
        End Try

        Return True
    End Function






 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
    Public Function ByteToImage(ByVal ByteArr() As Byte) As Image

        Dim ImageStream As MemoryStream

        Try
            If ByteArr.GetUpperBound(0) > 0 Then
                ImageStream = New MemoryStream(ByteArr)
                ByteToImage = Image.FromStream(ImageStream)
            Else
                ByteToImage = Nothing
            End If
        Catch ex As Exception
            ByteToImage = Nothing
        End Try

    End Function

沒有留言:

張貼留言