Emgu图片格式转换

①以文件流的方式去读取图片,读取出的图片可进行修改:

Public Function readImageFileByFileStream(ByVal filePath As String) As Image
    Using reader As New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        Return System.Drawing.Image.FromStream(reader)
    End Using
End Function

②bitmap转为Image

先将bitmap转为byte(),再将byte()转为image

Dim bgrImage_bytes As Byte() = bitmapToGifBytes(DirectCast(bgrImage, Emgu.CV.Image(Of Emgu.CV.Structure.Bgr, Byte)).Bitmap)
Dim bgrImage_image As Image = System.Drawing.Image.FromStream(New MemoryStream(bgrImage_bytes))
Public Shared Function bitmapToGifBytes(ByRef bitmap) As Byte()
        If bitmap Is Nothing Then
            Return Nothing
        End If
        Dim gifImageCodecInfo As ImageCodecInfo
        gifImageCodecInfo = GetEncoderInfo(ImageFormat.Gif)
        Dim gifEncoderParameter1 As EncoderParameter
        Dim gifEncoderParameter2 As EncoderParameter
        Dim gifEncoderParameters As EncoderParameters
        gifEncoderParameter1 = New EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1)
        gifEncoderParameter2 = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 0)
        gifEncoderParameters = New EncoderParameters(2)
        gifEncoderParameters.Param(0) = gifEncoderParameter1
        gifEncoderParameters.Param(1) = gifEncoderParameter2
        Dim ms As MemoryStream = New MemoryStream()
        bitmap.Save(ms, gifImageCodecInfo, gifEncoderParameters)
        Return ms.ToArray()
End Function 
Public Shared Function GetEncoderInfo(ByVal format As ImageFormat) As ImageCodecInfo
        Dim j As Integer
        Dim encoders() As ImageCodecInfo
        encoders = ImageCodecInfo.GetImageEncoders()
        j = 0
        While j < encoders.Length
            If encoders(j).FormatID = format.Guid Then
                Return encoders(j)
            End If
            j += 1
        End While
        Return Nothing
End Function



③byte()转为cvImage、Image

'因为保存到DB中时,对byte进行了压缩,所以需先解压缩为byte()再进行转换
Dim bytes As Byte()
bytes = Uncompress(DirectCast(pictureImage.Bytes, Byte()))
'byte文件转换为cvImage
Dim img As New Image(Of Gray, Byte)(Image.FromStream(New MemoryStream(bytes)))
Dim drawingimage As Image = System.Drawing.Image.FromStream(New MemoryStream(bytes))
Function Uncompress(questionImage) As Byte()
    ' Clean up memory with Using-statements.
    Dim decompressedBytes(Int(Val(questionImage.length.ToString)) * 2) As Byte
    'Dim StreamR As StreamReader
    Using memory As MemoryStream = New MemoryStream(DirectCast(questionImage, Byte()))
        ' Create compression stream.
        Using gzip As GZipStream = New GZipStream(memory, CompressionMode.Decompress, True)
            '实现从字符串进行读取
            gzip.Read(decompressedBytes, 0, decompressedBytes.Length)
            'StreamR = New StreamReader(gzip, Encoding.Default)
            'string str = streamR.ReadToEnd();
        End Using
        Return decompressedBytes.ToArray()
    End Using
End Function


④bitmap转byte()

Public Function bitmapToGifBytes(ByRef bitmap) As Byte()
        If bitmap Is Nothing Then
            Return Nothing
        End If
        Dim gifImageCodecInfo As ImageCodecInfo
        gifImageCodecInfo = GetEncoderInfo(ImageFormat.Gif)
        Dim gifEncoderParameter1 As EncoderParameter
        Dim gifEncoderParameter2 As EncoderParameter
        Dim gifEncoderParameters As EncoderParameters
        gifEncoderParameter1 = New EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1)
        gifEncoderParameter2 = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 0)
        gifEncoderParameters = New EncoderParameters(2)
        gifEncoderParameters.Param(0) = gifEncoderParameter1
        gifEncoderParameters.Param(1) = gifEncoderParameter2
        Dim ms As MemoryStream = New MemoryStream()
        bitmap.Save(ms, gifImageCodecInfo, gifEncoderParameters)
        Return ms.ToArray()
End Function
Public Shared Function GetEncoderInfo(ByVal format As ImageFormat) As ImageCodecInfo
        Dim j As Integer
        Dim encoders() As ImageCodecInfo
        encoders = ImageCodecInfo.GetImageEncoders()
        j = 0
        While j < encoders.Length
            If encoders(j).FormatID = format.Guid Then
                Return encoders(j)
            End If
            j += 1
        End While
        Return Nothing
End Function

⑤byte转为Image、Bitmap、Image(Of Gray, Byte)

byte转为Image

Dim drawingimage As Image = System.Drawing.Image.FromStream(New MemoryStream(bytes))
byte转为Bitmap

Dim img As Bitmap = Bitmap.FromStream(New MemoryStream(bytes))
byte转为Emgu的CVImage

Dim stream As MemoryStream = New MemoryStream(bytes)
Dim img As Image(Of Gray, Byte) = New Image(Of Gray, Byte)(Image.FromStream(stream))


你可能感兴趣的:(图片格式转换,EmguCV,Emgu,bitmap转Image,bitmap转CVImage)