Click here to Skip to main content
16,016,770 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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)
        {
            //load the original image
            Bitmap bmp = new Bitmap(sourceFileName);
            ArrayList newPalette = null; //receives the stretched palette
            Hashtable colorIndexToNewIndices = null; //recevies the list of new color indices

            //create a new palette from the existing one
            StretchPalette(bmp.Palette, maxPaletteSize, ref newPalette, ref colorIndexToNewIndices);

            //create a bitmap with the new palette and the hidden message
            Bitmap newBmp = CreateBitmap(bmp, newPalette, colorIndexToNewIndices, messageStream, keyStream);

            //save the new bitmap
            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);//Here i am getting Parameter is not valid exception
            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            //copy all pixels
            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;

            //read pixels until the message is complete
            while ((messageLength == 0) || (messageStream.Length < messageLength))
            {
                //locate the next pixel that carries a hidden bit
                pixelIndex += GetKey(keyStream);
                pixel = pixels[pixelIndex];

                if ((palette[pixel].B % 2) == 1)
                {
                    //odd blue-component: message-bit was "1"
                    messageByte += (byte)(1 << messageBitIndex);
                } //else: messageBit was "0", nothing to do

                if (messageBitIndex == 7)
                { //a byte is complete
                    //save and reset messageByte, reset messageBitIndex
                    messageStream.WriteByte(messageByte);
                    messageBitIndex = 0;
                    messageByte = 0;

                    if ((messageLength == 0) && (messageStream.Length == 4))
                    {
                        //message's length has been read
                        messageStream.Seek(0, SeekOrigin.Begin);
                        messageLength = new BinaryReader(messageStream).ReadInt32();
                        messageStream.SetLength(0);
                    }
                }
                else
                {
                    messageBitIndex++; //next bit
                }
            }

            //release the carrier bitmap
            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
Posted
Updated 30-Apr-16 23:46pm

1 solution

Start by checking the file itself: normally it's because when it was saved, it was saved badly, and it isn't a "real" image file.
Have a look at this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's based on DB stored images (because that's when the problem mostusually occurs) but have a good look at how you save the file as you quite likely have the same problem!
 
Share this answer
 
Comments
manigandan v 1-May-16 6:06am    
Thanks for your kind response :) yeah its working when i give the path of the image directly but i need to get the path of the uploaded image from the fileUpload control because i need to extract the message from the uploaded image.... but its not working :/
OriginalGriff 1-May-16 6:15am    
The FileUpload Control doesn't give you a path - it would be meaningless if it did as your server code can't access the client file system!
All it can give you is a file name - and it's up to you to save the data in the right place and remember where you put it. So look at the code where you handle the FileUpload data and see what it does.
I can't - I don't have access to your HDD!
manigandan v 1-May-16 6:14am    
@OriginalGriff i'm not using database in my program just encrypting a image and sending it to the user's mail id and the user will upload the received image and then i need to verify it by decrypting it
manigandan v 1-May-16 6:32am    
Will it work in html file upload?
Because i'm doing this as my mini project in my college so i need to verify the user received image...so pls suggest any other solutions for this problem :/
OriginalGriff 1-May-16 6:44am    
No - you cannot "pull" a file from the client, he has to initiate sending it to you.
Think about it: would you want your system to let any website you visit access the files on your hard disk? Security forbids this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900