2

I am converting images to byte array and storing in a text file using the following code. I am retrieving them successfully as well.

My concern is that the quality of the retrieved image is not up to the expectation. Is there a way to have better conversion to byte array and retrieving? I am not worried about the space conception.

Please share your thoughts.

    string plaintextStoringLocation = @"D:\ImageSource\Cha5.txt";
    string bmpSourceLocation = @"D:\ImageSource\Cha50.bmp";

    ////Read image
    Image sourceImg = Image.FromFile(bmpSourceLocation);

    ////Convert to Byte[]
    byte[] clearByteArray = ImageToByteArray(sourceImg);


    ////Store it for future use (in plain text form)
    StoreToLocation(clearByteArray, plaintextStoringLocation);

    //Read from binary
    byte[] retirevedImageBytes = ReadByteArrayFromFile(plaintextStoringLocation);

    //Retrieve from Byte[]
    Image destinationImg = ByteArrayToImage(retirevedImageBytes);

    //Display Image
    pictureBox1.Image = destinationImg;

EDIT: And the solution is - use Base64

            //Plain Text Storing Location
            string plaintextStoringLocation = @"D:\ImageSource\GirlInflower23.txt";
            string bmpSourceLocation = @"D:\ImageSource\GirlInflower1.bmp";

            ////Read image
            Image sourceImg = Image.FromFile(bmpSourceLocation);


            string base64StringOfIMage = ImageToBase64(sourceImg, ImageFormat.Bmp);

            byte[] byteOfString = Convert.FromBase64String(base64StringOfIMage);


            StoreToLocation(byteOfString, plaintextStoringLocation);

            byte[] retrievedBytesForStrimngForImage = ReadByteArrayFromFile(plaintextStoringLocation);


            MemoryStream memStream = new MemoryStream(retrievedBytesForStrimngForImage);
            //memStream.Read();

            Image retrievedImg = Image.FromStream(memStream);
            pictureBox1.Image = retrievedImg;
LCJ
  • 22,196
  • 67
  • 260
  • 418
Lijo
  • 63
  • 1
  • 6
  • 1
    What are ImageToByteArray, ReadByteArrayFromFile and ByteArrayToImage? Is this code you have written yourself? – Mark Byers Jun 17 '10 at 20:05
  • Are you using the same encoding when converting to `byte[]`, writing to and reading from the file? – Oded Jun 17 '10 at 20:07

2 Answers2

2

Yes, it is possible to get completely lossless storage. If you just store it in its original BMP format there will be no problem. I assume you are converting it to text because you want to send it via some protocol where binary characters will be corrupted.

Instead of whatever you are doing, you could consider using Convert.ToBase64String.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

I haven't had any problems with this fragment...try it...if you get good results then the problem is in your Image -> byte[] or byte[] -> Image code :)

Image srcImage;
Image destImage;

// load an image
srcImage = Image.FromFile(filename);

// save the image via stream -> byte[]
using(MemoryStream stream = new MemoryStream()){
   image.Save(stream, ImageFormat.xxx);
   byte[] saveArray = stream.ToArray();
   /*..... strore saveArray......*/
}

// rehydrate
byte[] loadArray = /*...get byte array from storage...*/

using(MemoryStream stream = new MemeoryStream(loadArray)){
  destImage = Image.FromStream(stream);
}

pictureBox.Image = dstImage;

// don't forget...dispose of any Image/Stream objects 
Rusty
  • 3,228
  • 19
  • 23