65.9K
CodeProject 正在变化。 阅读更多。
Home

将图像文件转换为字节并反向转换

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.86/5 (11投票s)

2008年12月21日

CPOL
viewsIcon

133977

这是一小段代码,可以将图像文件转换为字节数组,然后以图像类型存储到数据库中。

引言

我在一个项目中需要将图像文件以字节数组的形式存储到数据库中时编写了这段代码。这些字节被保存到数据库的图像类型列中。

Using the Code

这是将图像文件转换为字节()的函数:

        ''' <summary>
        ''' Converts the Image File to array of Bytes
        ''' </summary>
        ''' <param name="ImageFilePath">The path of the image file</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
            Dim _tempByte() As Byte = Nothing
            If String.IsNullOrEmpty(ImageFilePath) = True Then
                Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
                Return Nothing
            End If
            Try
                Dim _fileInfo As New IO.FileInfo(ImageFilePath)
                Dim _NumBytes As Long = _fileInfo.Length
                Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
                Dim _BinaryReader As New IO.BinaryReader(_FStream)
                _tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
                _fileInfo = Nothing
                _NumBytes = 0
                _FStream.Close()
                _FStream.Dispose()
                _BinaryReader.Close()
                Return _tempByte
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

这是将字节数组转换为图像文件的函数:

        ''' <summary>
        ''' Converts array of Bytes to Image File
        ''' </summary>
        ''' <param name="ImageData">The array of bytes which contains image binary data</param>
        ''' <param name="FilePath">The destination file path.</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ConvertBytesToImageFile(ByVal ImageData As Byte(), ByVal FilePath As String) As Result
            If IsNothing(ImageData) = True Then
                Return Result.Failure
                'Throw New ArgumentNullException("Image Binary Data Cannot be Null or Empty", "ImageData")
            End If
            Try
                Dim fs As IO.FileStream = New IO.FileStream(FilePath, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
                Dim bw As IO.BinaryWriter = New IO.BinaryWriter(fs)
                bw.Write(ImageData)
                bw.Flush()
                bw.Close()
                fs.Close()
                bw = Nothing
                fs.Dispose()
                Return Result.Success
            Catch ex As Exception
                Return Result.Failure
            End Try
        End Function

在编写这段代码时,我遇到一个非常有用的函数,也将其附在此处。这段代码用于将字节和图像文件转换为内存流对象

	''' <summary>
        ''' Converts array of bytes to Memoey Stream
        ''' </summary>
        ''' <param name="ImageData">The array of bytes which contains image binary data</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ConvertBytesToMemoryStream(ByVal ImageData As Byte()) As IO.MemoryStream
            Try
                If IsNothing(ImageData) = True Then
                    Return Nothing
                    'Throw New ArgumentNullException("Image Binary Data Cannot be Null or Empty", "ImageData")
                End If
                Return New System.IO.MemoryStream(ImageData)
            Catch ex As Exception
                Return Nothing
            End Try
        	End Function


        
	''' <summary>
        ''' Converts the Image File to Memory Stream
        ''' </summary>
        ''' <param name="ImageFilePath"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ConvertImageFiletoMemoryStream(ByVal ImageFilePath As String) As IO.MemoryStream
            If String.IsNullOrEmpty(ImageFilePath) = True Then
                Return Nothing
                ' Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
            End If
            Return ConvertBytesToMemoryStream(ConvertImageFiletoBytes(ImageFilePath))
        End Function

关注点

这是一种从数据库中直接保存和编辑图像的好方法。主要的缺点是它可能会占用数据库中的大量内存空间,使数据库变得非常庞大。

参考文献

我查阅了网络上的一些示例和 MSDN 文档。

© . All rights reserved.