Lijo
Lijo

Reputation: 63

C# - High Quality Byte Array Conversion of Images

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;

Upvotes: 2

Views: 2674

Answers (2)

Rusty
Rusty

Reputation: 3268

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 

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838186

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.

Upvotes: 2

Related Questions