大家好。我在将文本隐藏到 Jpeg 图像时遇到问题。我将图像转换为二进制,将文本转换为二进制。我想用我的字符串消息中的一个位交换图像中每个字节的最后一位,但问题是图像没有成功显示。
这是我正在使用的代码:
Dim myImage As Image = Image.FromFile(Server.MapPath("Images/image.jpg"))
'Image to byte[]
Dim imgMemoryStream As MemoryStream = New MemoryStream()
Dim imgByteArray As Byte() = Nothing
myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
imgByteArray = imgMemoryStream.GetBuffer()
'Byte[] to image
imgMemoryStream = New IO.MemoryStream(imgByteArray)
myImage = Drawing.Image.FromStream(imgMemoryStream)
'Show it and check if the image is accordant with E:\VBproject\1.jpg.
Dim ln As Long
Dim str2 As String = "h"
Dim strArry As Byte() = New Byte(str2.Length) {}
strArry = System.Text.Encoding.Unicode.GetBytes(str2)
Dim i As Integer = 0
Dim b As Long = ((UBound(strArry) + 1) * 8) - 1
Dim Binstr(b) As String
For Each strbt As Byte In strArry
Dim binnarystr As String = Convert.ToString(strbt, 2).PadLeft(8, "0"c)
For Each Chr As Char In binnarystr
Binstr(i) += Chr
i += 1
Next
Next
'=====================================================
Dim ii As Integer = 1
'=================================================================
i = 0
Dim NewImageArray(imgByteArray.Length) As Byte
For Each Bt As Byte In imgByteArray
If i < UBound(Binstr) + 1 Then
Dim Bin As String = Convert.ToString(Bt, 2).PadLeft(8, "0"c)
Dim str As String
Bin = Bin.Remove(7, 1)
str = Bin.ToString() & Binstr(i).ToString()
ii = 1
ln = 0
For Each Chr As Char In str
If ii = 1 Then
ln += CInt(Chr.ToString()) * 128
ElseIf ii = 2 Then
ln += CInt(Chr.ToString()) * 64
ElseIf ii = 3 Then
ln += CInt(Chr.ToString()) * 32
ElseIf ii = 4 Then
ln += CInt(Chr.ToString()) * 16
ElseIf ii = 5 Then
ln += CInt(Chr.ToString()) * 8
ElseIf ii = 6 Then
ln += CInt(Chr.ToString()) * 4
ElseIf ii = 7 Then
ln += CInt(Chr.ToString()) * 2
ElseIf ii = 8 Then
ln += CInt(Chr.ToString()) * 1
End If
ii += 1
Next
NewImageArray(i) = ln
ii = 1
ln = 0
Else
NewImageArray(i) = Bt
End If
i += 1
Next
My.Computer.FileSystem.WriteAllBytes(Server.MapPath("Images/Alpha.jpg"), NewImageArray, False)