Zak D
Zak D

Reputation: 31

C# byte array to image not working with identical byte arrays

The complete code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

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

        //Display byte array as image
        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            Image img = ByteArrayToImage(File.ReadAllBytes("")); //fill with path info

            pictureBox1.Image = (Image)img.Clone();
        }

        //Convert to image from bytes
        public Image ByteArrayToImage(byte[] byteArrayIn)
        {
            using (MemoryStream ms = new MemoryStream(byteArrayIn))
            {
                ms.Position = 0;
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
        }

        //Open Image for conversion
        private void loadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            DialogResult dr = new DialogResult();
            dr = opf.ShowDialog();
            if (dr == DialogResult.OK)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromFile(opf.FileName);
            }
        }

        private void convertImage_Click(object sender, EventArgs e)
        {
            //Choose Path to Save To
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Title = "Choose Directory and File Name";
            saveDialog.Filter = "Canga Comix *.CCMX|*.CCMX";
            DialogResult dr = saveDialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                byte[] array;

                //Save Image
                using (MemoryStream ms = new MemoryStream())
                {
                    Image img = pictureBox1.Image;
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    array = ms.ToArray();
                }

                using(FileStream fs = new FileStream(saveDialog.FileName, FileMode.Create))
                {
                    fs.Write(array, 0, array.Length);
                }
            }
        }

        //clear image
        private void clearImage_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
        }
    }
}

These are pretty standard. I have a test program that uses these methods. It opens an image in a pictureBox and then converts it to byte[] and clears the pictureBox Image. Then you can hit Load byte[] and it will display the picture properly as it runs the ByteArrayToImage method.

Then if I save out a picture from my other program and try to open it in the test program it gives me the unholy "Parameter is not valid" error. Even though both text documents are exactly the same as far as I can tell.

Upvotes: 3

Views: 978

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283921

This code contains a common problem. Here you create an Image bound to a stream containing your bitmap...

public Image ByteArrayToImage(byte[] byteArrayIn)
{
    using (MemoryStream ms = new MemoryStream(byteArrayIn))
    {
        ms.Position = 0;
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
}

... and the using block disposes the MemoryStream instance on the way out, which makes the Image rather useless.

You can get an Image that manages its own memory instead of expecting your stream to stick around by calling Clone():

public Image ByteArrayToImage(byte[] byteArrayIn)
{
    using (MemoryStream ms = new MemoryStream(byteArrayIn))
    using (Image returnImage = Image.FromStream(ms))
    {
        return returnImage.Clone();
    }
}

The clone isn't bound to the original stream.

Upvotes: 3

Related Questions