Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media.Imaging;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        public BitmapImage ToImage(byte[] array)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(array))
            {
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = ms;
                image.EndInit();
                return image;
            }
        }
        public static byte[] ImageToBinary(string imagePath)
        {
            FileStream fS = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
            byte[] b = new byte[fS.Length];
            fS.Read(b, 0, (int)fS.Length);
            fS.Close();
            return b;
        }
        public static string StringToBinary(string data)
        {
            StringBuilder sb = new StringBuilder();

            foreach (char c in data.ToCharArray())
            {
                sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
            }
            return sb.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {


  
               string result = System.Text.Encoding.ASCII.GetString(ImageToBinary(openFileDialog1.FileName));

               System.IO.File.WriteAllText(@"C:\Users\Lenovo\Desktop\b.txt", result);
            }
        }
        public static Bitmap ByteToImage(byte[] blob)
        {
            MemoryStream mStream = new MemoryStream();
            byte[] pData = blob;
            mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
            Bitmap bm = new Bitmap(mStream, false);
            mStream.Dispose();
            return bm;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string input = File.ReadAllText(openFileDialog1.FileName);
                byte[] toBytes = Encoding.ASCII.GetBytes(input);
               // System.IO.File.WriteAllText(@"C:\Users\Lenovo\Desktop\a.bmp", ToImage(toBytes));

                BitmapImage bmp1 = ToImage(toBytes);

                pictureBox1.Image = ByteToImage(toBytes);
              
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
           // SaveFileDialog sfd = new SaveFileDialog();
            Bitmap varBmp = new Bitmap(pictureBox1.Image);
            Bitmap newBitmap = new Bitmap(varBmp);
            varBmp.Dispose();
            varBmp = null;

           

            saveFileDialog1.Filter = "jpeg dosyası(*.jpg)|*.jpg|Bitmap(*.bmp)|*.bmp";


            saveFileDialog1.Title = "Kayıt İşlemi";


            saveFileDialog1.FileName = "resim";


            DialogResult sonuç = saveFileDialog1.ShowDialog();


            if (sonuç == DialogResult.OK)
            {
                newBitmap.Save(saveFileDialog1.FileName,ImageFormat.Bmp);
            }
        }
    }
}


What I have tried:

For Example suppose that we have an image below

t — Postimage.org[^]


convert image to ascii string and save as txt.I want to do reverse process.I read ascii text and convert to an original image but It is not fully formed.Like this.Where is my error

resim — Postimage.org[^]
Posted
Updated 28-Nov-16 17:28pm
v2

1 solution

Your error is in the conversion process. ASCII.ToString does not convert binary data into text - it creates a string that contains binary data. For that, you need an encoding algorithm such as UUEncode or Base64.

The Convert class has simple Base64 conversion methods. So once you have your byte array from the image, perform
C#
string text = Convert.ToBase64String(data)

and to go the other way
C#
byte[] data = Convert.FromBase64String(text)


You can then read/write the file with ASCII encoding by opening a StreamReader or StreamWriter with the Encoding.ASCII parameter.
 
Share this answer
 
Comments
Rıza Berkay Ayçelebi 30-Nov-16 1:44am    
if ı use Base64String text file size bigger than image file.I want text size and image size equal
Midi_Mick 30-Nov-16 2:31am    
You can't do that, unless you perform some sort of compression (in which case, the file is no longer text). The problem is that there are 256 possible byte characters, but only 96 ASCII text characters. To make the file sizes equal, you need to use binary characters, which is the image itself.
Rıza Berkay Ayçelebi 30-Nov-16 4:48am    
I thought I can do compression for image like this but I am confused now.How to compress bitmap file with huffman and rle algorithm.you said "To make the file sizes equal, you need to use binary characters, which is the image itself." how can ı do that can you help me or can you send me code
Midi_Mick 30-Nov-16 5:02am    
Yes - just forget about trying to convert to text - read the image data as a byte array, and save it to disk as that byte array unchanged.
If you want to apply huffman or rle compression, forget about text altogether and look up the algorithms (google "huffman image compression c#"). Apply them to your byte array before you save it. Then decompress using the appropriate algorithm when you read the data back.
Rıza Berkay Ayçelebi 30-Nov-16 11:01am    
So ı dont find a source code for hfufman for image everywhere text.
For image can ı compress rgb code ?

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