Here is my code:
public void btnExtract_Click(object sender, System.EventArgs e)
{
MemoryStream message = new MemoryStream();
FileStream key = new FileStream(@"E:\studies\sem8 projects\count.txt", FileMode.Open);
PaletteUtility util = new PaletteUtility(Server.MapPath(@"~/mini/" + upload.FileName),null);
util.Extract(message, key);
message.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(message);
Label3.Text = reader.ReadToEnd();
String ch = Label3.Text;
if (ch == "hello")
Response.Write("correct image");
reader.Close();
key.Close();
}
}
public class PaletteUtility
{
public String sourceFileName;
private String destinationFileName;
public long CountUseableUnits(Stream keyStream)
{
long countUseableUnits = 0;
long unitIndex = 0;
byte key;
Bitmap bmp = new Bitmap(sourceFileName);
long countUnits = bmp.Width * bmp.Height;
bmp.Dispose();
while (true)
{
key = GetKey(keyStream);
if (unitIndex + key < countUnits)
{
unitIndex += key;
countUseableUnits++;
}
else
{
break;
}
}
keyStream.Seek(0, SeekOrigin.Begin);
return countUseableUnits;
}
public void Hide(int maxPaletteSize, Stream messageStream, Stream keyStream)
{
Bitmap bmp = new Bitmap(sourceFileName);
ArrayList newPalette = null;
Hashtable colorIndexToNewIndices = null;
StretchPalette(bmp.Palette, maxPaletteSize, ref newPalette, ref colorIndexToNewIndices);
Bitmap newBmp = CreateBitmap(bmp, newPalette, colorIndexToNewIndices, messageStream, keyStream);
newBmp.Save(destinationFileName);
newBmp.Dispose();
bmp.Dispose();
}
public void Extract(Stream messageStream, Stream keyStream)
{
System.Drawing.Image image;
image = BitmapFromWeb(@sourceFileName);
Bitmap bmp = new Bitmap(image);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
byte[] pixels = new byte[bmpData.Stride * bmpData.Height];
Marshal.Copy(bmpData.Scan0, pixels, 0, pixels.Length);
Color[] palette = bmp.Palette.Entries;
byte messageByte = 0, messageBitIndex = 0, pixel = 0;
int messageLength = 0, pixelIndex = 0;
while ((messageLength == 0) || (messageStream.Length < messageLength))
{
pixelIndex += GetKey(keyStream);
pixel = pixels[pixelIndex];
if ((palette[pixel].B % 2) == 1)
{
messageByte += (byte)(1 << messageBitIndex);
}
if (messageBitIndex == 7)
{
messageStream.WriteByte(messageByte);
messageBitIndex = 0;
messageByte = 0;
if ((messageLength == 0) && (messageStream.Length == 4))
{
messageStream.Seek(0, SeekOrigin.Begin);
messageLength = new BinaryReader(messageStream).ReadInt32();
messageStream.SetLength(0);
}
}
else
{
messageBitIndex++;
}
}
bmp.UnlockBits(bmpData);
bmp.Dispose();
}
What I have tried:
I'm just trying to extract the message from the uploaded image file.But it is showing parameter is not valid exception in the bitmap constructor