示例#1
0
        /**
         * Combines two directories into an output directory.
         * <P>
         * The output directory (tempdir) will have all the original
         * images plus all the stegoimages copied into it.
         *
         * @param messagedir The directory full of messages in (txt files).
         * @param imagedir The directory full of images (png, jpg, bmp).
         * @param tempdir The directory to write the results to.
         * @param algorithm The algorithm to use to combine the messages and
         * images with.
         * @throws IllegalArgumentException If any directories are null.
         * @return A string full of skipped files.
         */

        public string CreateCombineDirectories(string messagedir, string imagedir, string tempdir,
                                               SteganographicAlgorithmImpl algorithm)
        {
            if (!Directory.Exists(messagedir) || !Directory.Exists(imagedir) || !Directory.Exists(tempdir))
            {
                throw new ArgumentException("Must be directories!");
            }

            var    errors = new StringBuilder("Errors: \n========\n\n");
            var    messageList = Directory.GetFiles(messagedir);
            var    imagelist = Directory.GetFiles(imagedir);
            string coverfilepath, messagefilepath, outputpath, originalname;
            var    fileseperator = Path.DirectorySeparatorChar;

            //	//for each file in the image folder...
            foreach (var imagefile in imagelist)
            {
                if (imagefile.EndsWith(".bmp") || imagefile.EndsWith(".jpg") || imagefile.EndsWith(".png"))
                {
                    //ok to combine it...
                    coverfilepath = imagedir;
                    originalname  = imagefile.Substring(0, imagefile.IndexOf("."));
                    //for each message in the message folder...
                    foreach (var messagefile in messageList)
                    {
                        try
                        {
                            if (messagefile.EndsWith(".txt"))
                            {
                                //ok to combine...
                                messagefilepath = messagefile;

                                //setup the two files...

                                //setup the filename...
                                outputpath = originalname + "~" + messagefile.Substring(0, messagefile.LastIndexOf(".")) +
                                             "-"
                                             +
                                             algorithm.GetType()
                                             .Name.ToLower()
                                             .Substring(algorithm.GetType().Name.LastIndexOf(".") + 1,
                                                        algorithm.GetType().Name.Length) + "." + "png";

                                //write it
                                new Bitmap(algorithm.Encode(coverfilepath,
                                                            new TextMessage(File.ReadAllText(messagefilepath)), 0)).Save(tempdir);
                            }
                        }
                        catch (Exception)
                        {
                            //just go onto the next one...
                            errors.Append("Error: Could not process: " + imagefile + " with " + messagefile + "\n");
                        }
                        //end image loop
                        //cleanup to speed up memory
                        GC.Collect();
                    }
                }
            }

            return(errors.ToString());
        }