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 .
- LoadText(click on com) : you can see your text in image like top image.
- SaveImage:Click on image,save it.
- 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))
{
var line = ""; var R = 0; var G = 0; var B = 0; var letters = new List<int>();
while ((line = sr.ReadLine()) != null)
{
foreach (char ch in line) {
letters.Add(Convert.ToInt16(ch));
}
letters.Add(255); }
var square = Math.Sqrt(letters.Count / 3);
square += 1; square = Math.Round(square);
var bmp = new Bitmap((int)square, (int)square);
var count = 0;
for (int row = 1; row < square; row++) {
for (int column = 1; column < square; column++) {
if (count < (letters.Count - 3)) {
R = letters[count++]; G = letters[count++]; B = letters[count++]; bmp.SetPixel(row, column, (Color.FromArgb( R, G, B)));
}
}
}
bmp.Save(FileImage, ImageFormat.Png);
}
}
}
</int>
This class get RGB color list And Convert the character
- Get Red->Convert the character
- Get Green->Convert the character
- Get Blue->Convert the character
class decom
{
public void doDeCompress(string FileImage,string FileText)
{
var lettersExtract = new List<int>();
var bmp = new Bitmap(Bitmap.FromFile(FileImage));
for (int row = 1; row < bmp.Width; row++) {
for (int column = 1; column < bmp.Height; column++)
{
var cr = bmp.GetPixel(row, column);
lettersExtract.Add(cr.R);
lettersExtract.Add(cr.G);
lettersExtract.Add(cr.B);
}
}
using (System.IO.StreamWriter file = new
System.IO.StreamWriter(FileText,false,Encoding.Default))
{
foreach (int write in lettersExtract)
{
if (write == 255)
file.WriteLine("");
else
file.Write((Char)write);
}
}
}
}
</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"; openFileDialog1.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialog1.Title = "Load text";
openFileDialog1.Filter = "Text Normal|*.txt|Text INF|*.inf";
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Chosen_File = openFileDialog1.FileName;
richTextBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);
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)
{
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 {
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)
{
Chosen_File = saveFileDialog1.FileName;
pictureBox1.Image.Save(Chosen_File);
MessageBox.Show("Image Save it");
}
}
catch
{
MessageBox.Show("not find any image in pictureBox1");
}
}
}
}