Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Save Text Data In image

0.00/5 (No votes)
23 Oct 2014 2  
Save Text Data In image Format

Text Save like this:

Introduction

i try write new string compressor but believe me it is not easy , i try save text in image RGB and move it in Gray but i find is not possible or get more bytes . In any case  i send this code for you. maybe save your time .

  1. LoadText(click on com) : you can see your text in image like top image.
  2. SaveImage:Click on image,save it.
  3. LaodImage(Click on decom):you can save text save in image.

MainClass:

you can use  class  com & decom 

                com nbv = new com();
                nbv.doCompress(String, String);

Class Decom image

                decom nb = new decom();
                nb.doDeCompress(String,String);

 

Background

 

Using the code

This class put int word in RGB(X1,X2,X3) its less <255 ,Get square root of total letter that indicate size of the image  the value divided by three since each pixel contains 3 letter.

Quote:
Convert the character into ASCII value and add in letters collection
Mixed RGB IN Pixel(R,G,b)
   class com
    {
        public void doCompress(string FileText, string FileImage)
        {

            using (StreamReader sr = File.OpenText(FileText)) 
            // Get text from a .txt file to compress
            {

                var line = ""; // To hold a line of the file
                var R = 0; // To hold 4 letters as Red value
                var G = 0; // To hold 4 letters as Green value
                var B = 0; // To hold 4 letters as Blue value
                var letters = new List<int>(); 
                // To hold the characters of line
                //Read lines one by one from stream reader
                while ((line = sr.ReadLine()) != null)

                {
                    foreach (char ch in line) // Get character from a line
                    {
                        letters.Add(Convert.ToInt16(ch)); 
                        // Convert the character
                        //into ASCII value and add in letters collection
                    }

                    letters.Add(255); // Use 255 as a flag that indicates new line
                    //counter_line++;
                }

               /* Get square root of total letter that indicate size 
                of the image (get the value divided by three since each 
                pixel contains 3 letters*/
                var square = Math.Sqrt(letters.Count / 3);
                /* Get square root of total letter that indicate size 
                 * of the image (get the value divided by three since
                 * each pixel contains 3 letters*/
                square += 1; // Add one value for any exceeds if any
                square = Math.Round(square);
                // Have a BitMap with optimal size that we calculated
                var bmp = new Bitmap((int)square, (int)square);
                var count = 0; // count for letters

                for (int row = 1; row < square; row++) // Indicates row number
                {
                    for (int column = 1; column < square; column++) // Indicate column number
                    {
                        if (count < (letters.Count - 3)) // Check for last pixel
                        {
                            R = letters[count++]; // Assignee a letter in Red value
                            G = letters[count++]; // Assignee a letter in Green value
                            B = letters[count++]; // Assignee a letter in Blue value
                            // Set pixel with the combination of two value
                            bmp.SetPixel(row, column, (Color.FromArgb( R, G, B))); 
                        }
                    }
                }
              
            
                    bmp.Save(FileImage, ImageFormat.Png); //Save the Bitmap and this is the compressed Image Data
         
             
            }
        }

    }
</int>

This class get RGB color list And Convert the character

  1. Get Red->Convert the character
  2. Get Green->Convert the character
  3. Get Blue->Convert the character
   class decom
    {
        public void doDeCompress(string FileImage,string FileText)
        {

            var lettersExtract = new List<int>(); // To hold extracted letters

            var bmp = new Bitmap(Bitmap.FromFile(FileImage)); 
            // Get the Image data to DeCompress

            for (int row = 1; row < bmp.Width; row++) // Indicates row number
            {

                for (int column = 1; column < bmp.Height; column++) 
               // Indicate column number
                {

                    var cr = bmp.GetPixel(row, column); 
                   // Get the pixel of the current row and column

                    lettersExtract.Add(cr.R); 
                   // Get the Red Value and Add in letterExtract collection

                    lettersExtract.Add(cr.G); 
                   // Get the Green Value and Add in letterExtract collection

                    lettersExtract.Add(cr.B); 
                   // Get the Blue Value and Add in letterExtract collection

                }

            }

            using (System.IO.StreamWriter file = new 
            System.IO.StreamWriter(FileText,false,Encoding.Default)) 
                 // Open a text file to extract
            {

                foreach (int write in lettersExtract)
              //Get color value one by one from the letters Extracted
                {
                    if (write == 255) 
              //Condition check for new line since 255 is a flag of new line
                        file.WriteLine("");
                    else
                        file.Write((Char)write); 
              // Write the character in the file by converting 
              //  the color value into   character

                }

            }

        }
    }
</int>

Mix all data in GUI Windows:

i use openFileDialog and saveFileDialog .

  • load text in RichTextBoxStream
  • load temp image in PictureBox(PictureBoxSizeMode.StretchImage)
  • click on image and save it(AppDomain.CurrentDomain.BaseDirectory ,pictureBox.Image.Save(URL) ).
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static string word;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Chosen_File = "";
            string name_image_save = AppDomain.CurrentDomain.BaseDirectory.ToString() 
             + "\\" + "compressed.png";  // temp file
            openFileDialog1.InitialDirectory = Application.ExecutablePath.ToString();
            openFileDialog1.Title = "Load text";
            openFileDialog1.Filter = "Text Normal|*.txt|Text INF|*.inf";
            if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                //richTextBox1
                Chosen_File = openFileDialog1.FileName;
                richTextBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);
                //-------------- use class 
                com nbv = new com();
                nbv.doCompress(Chosen_File, name_image_save);
                //------------
            }
            pictureBox1.Load(name_image_save);
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

        }

        private void button2_Click(object sender, EventArgs e)
        {

            string Chosen_File = "";
            string name_text_save = AppDomain.CurrentDomain.BaseDirectory.ToString()
             + "\\" + "decompress.txt";
            openFileDialog2.InitialDirectory = Application.ExecutablePath.ToString();
            openFileDialog2.Title = "Load image";
            openFileDialog2.Filter = "Image Normal|*.PNG";
            if (openFileDialog2.ShowDialog() != DialogResult.Cancel)
            {
                //richTextBox1
                Chosen_File = openFileDialog2.FileName;
                pictureBox2.Load(Chosen_File);
                //-------------------
                decom nb = new decom();
                nb.doDeCompress(Chosen_File, name_text_save);
                //--------------------
                pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                richTextBox2.LoadFile(name_text_save, RichTextBoxStreamType.PlainText);
            }
               
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try // control error
            {
                string Chosen_File = "";
                string name_image_save = AppDomain.CurrentDomain.BaseDirectory.ToString() 
                + "\\" + "compressed.png";
                saveFileDialog1.InitialDirectory = Application.ExecutablePath.ToString();
                saveFileDialog1.Title = "Save Image";
                saveFileDialog1.Filter = "Image Normal|*.PNG";
                if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                {
                    //richTextBox1
                    Chosen_File = saveFileDialog1.FileName;

                    pictureBox1.Image.Save(Chosen_File);
                    MessageBox.Show("Image Save it");

                }
            }
            catch
            {
                MessageBox.Show("not find any image in pictureBox1");
            }
        }

    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here