This function takes in bytes from an OLE field and returns the image bytes. It searches for common image header identifiers like "BM" for bitmap, JPEG and PNG markers, "GIF8" for GIF, and "II*" for TIFF. If a match is found, it extracts the image bytes from the specified index to the end of the OLE byte array. If no header is found, an exception is thrown.
This function takes in bytes from an OLE field and returns the image bytes. It searches for common image header identifiers like "BM" for bitmap, JPEG and PNG markers, "GIF8" for GIF, and "II*" for TIFF. If a match is found, it extracts the image bytes from the specified index to the end of the OLE byte array. If no header is found, an exception is thrown.
This function takes in bytes from an OLE field and returns the image bytes. It searches for common image header identifiers like "BM" for bitmap, JPEG and PNG markers, "GIF8" for GIF, and "II*" for TIFF. If a match is found, it extracts the image bytes from the specified index to the end of the OLE byte array. If no header is found, an exception is thrown.
Friend Shared Function GetImageBytesFromOLEField(ByVal oleFieldBytes() As Byte)
As Byte()
Dim BITMAP_ID_BLOCK As String = "BM"
Dim JPG_ID_BLOCK As String = ChrW(&HFF).ToString() & ChrW(&HD8).ToString() & ChrW(&HFF).ToString() Dim PNG_ID_BLOCK As String = ChrW(&H89).ToString() & "PNG" & vbCrLf & ChrW(&H1A).ToString() & vbLf Dim GIF_ID_BLOCK As String = "GIF8" Dim TIFF_ID_BLOCK As String = "II*" & ChrW(&H0).ToString()
Dim imageBytes() As Byte
' Get a UTF7 Encoded string version
Dim u7 As System.Text.Encoding = System.Text.Encoding.UTF7 Dim strTemp As String = u7.GetString(oleFieldBytes) Dim st2 As String = System.Text.Encoding.UTF8.GetString(oleFieldBytes) ' Get the first 300 characters from the string Dim strVTemp As String = strTemp.Substring(0, 300)
' Search for the block
Dim iPos As Integer = -1 If strVTemp.IndexOf(BITMAP_ID_BLOCK) <> -1 Then iPos = strVTemp.IndexOf(BITMAP_ID_BLOCK) ElseIf strVTemp.IndexOf(JPG_ID_BLOCK) <> -1 Then iPos = strVTemp.IndexOf(JPG_ID_BLOCK) ElseIf strVTemp.IndexOf(PNG_ID_BLOCK) <> -1 Then iPos = strVTemp.IndexOf(PNG_ID_BLOCK) ElseIf strVTemp.IndexOf(GIF_ID_BLOCK) <> -1 Then iPos = strVTemp.IndexOf(GIF_ID_BLOCK) ElseIf strVTemp.IndexOf(TIFF_ID_BLOCK) <> -1 Then iPos = strVTemp.IndexOf(TIFF_ID_BLOCK) Else Throw New Exception("Unable to determine header size for the OLE Object") End If
' From the position above get the new image
If iPos = -1 Then Throw New Exception("Unable to determine header size for the OLE Object") End If
imageBytes = New Byte(CInt(oleFieldBytes.LongLength - iPos - 1)) {}