Hi Every One I try to Make Method that work To Hide Text into Image It works Fine in PNG and Gif Extensions but it's not work with jpg image here is the method i work with it Can any one Help me to make it work in Jpg Image Extensions
Private Shared Sub HideOrExtract(ByRef messageStream As Stream, bitmap As Bitmap, keyStream As Stream, extract As Boolean, useGrayscale As Boolean)
Dim currentStepWidth As Integer = 0
Dim currentKeyByte As Byte
Dim currentReverseKeyByte As Byte
Dim keyPosition As Long
Dim bitmapWidth As Integer = bitmap.Width - 1
Dim bitmapHeight As Integer = bitmap.Height - 1
Dim currentColorComponent As Integer = 0
Dim pixelColor As Color
Dim messageLength As Int32
If extract Then
pixelColor = bitmap.GetPixel(0, 0)
messageLength = (pixelColor.R << 2) + (pixelColor.G << 1) + pixelColor.B
messageStream = New MemoryStream(messageLength)
Else
messageLength = CType(messageStream.Length, Int32)
If messageStream.Length >= 16777215 Then
Dim exceptionMessage As String = "Themessage is too long, only 16777215 bytes are allowed."
Throw New Exception(exceptionMessage)
End If
Dim countPixels As Long = (bitmapWidth * bitmapHeight) - 1
Dim countRequiredPixels As Long = 1
While (keyStream.Position < keyStream.Length) AndAlso (keyStream.Position < messageLength)
countRequiredPixels += keyStream.ReadByte()
End While
countRequiredPixels *= CLng(Math.Truncate(System.Math.Ceiling((CSng(messageStream.Length) / CSng(keyStream.Length)))))
If countRequiredPixels > countPixels Then
Dim exceptionMessage As String = "The image is too small for this message and key. " & countRequiredPixels & " pixels are required."
Throw New Exception(exceptionMessage)
End If
Dim colorValue As Integer = messageLength
Dim red As Integer = colorValue >> 2
colorValue -= red << 2
Dim green As Integer = colorValue >> 1
Dim blue As Integer = colorValue - (green << 1)
pixelColor = Color.FromArgb(red, green, blue)
bitmap.SetPixel(0, 0, pixelColor)
End If
keyStream.Seek(0, SeekOrigin.Begin)
messageStream.Seek(0, SeekOrigin.Begin)
Dim pixelPosition As New Point(1, 0)
For messageIndex As Integer = 0 To messageLength - 1
If keyStream.Position = keyStream.Length Then
keyStream.Seek(0, SeekOrigin.Begin)
End If
currentKeyByte = CByte(keyStream.ReadByte())
currentStepWidth = If((currentKeyByte = 0), CByte(1), currentKeyByte)
keyPosition = keyStream.Position
keyStream.Seek(-keyPosition, SeekOrigin.[End])
currentReverseKeyByte = CByte(keyStream.ReadByte())
keyStream.Seek(keyPosition, SeekOrigin.Begin)
While currentStepWidth > bitmapWidth
currentStepWidth -= bitmapWidth
pixelPosition.Y += 1
End While
If (bitmapWidth - pixelPosition.X) < currentStepWidth Then
pixelPosition.X = currentStepWidth - (bitmapWidth - pixelPosition.X)
pixelPosition.Y += 1
Else
pixelPosition.X += currentStepWidth
End If
pixelColor = bitmap.GetPixel(pixelPosition.X, pixelPosition.Y)
If extract Then
Dim foundByte As Byte = CByte(currentReverseKeyByte Xor GetColorComponent(pixelColor, currentColorComponent))
messageStream.WriteByte(foundByte)
currentColorComponent = If((currentColorComponent = 2), 0, (currentColorComponent + 1))
Else
Dim currentByte As Integer = messageStream.ReadByte() Xor currentReverseKeyByte
If useGrayscale Then
pixelColor = Color.FromArgb(currentByte, currentByte, currentByte)
Else
SetColorComponent(pixelColor, currentColorComponent, currentByte)
currentColorComponent = If((currentColorComponent = 2), 0, (currentColorComponent + 1))
End If
bitmap.SetPixel(pixelPosition.X, pixelPosition.Y, pixelColor)
End If
Next
bitmap = Nothing
keyStream = Nothing
End Sub