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

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;
        }
    }
}

      

It's pretty standard. I have a test program that uses these methods. It opens the image in a pictureBox and then converts it to byte [] and clears the image of the PictureBox. Then you can hit Load byte [] and it will display the image correctly as it runs the ByteArrayToImage method.

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

+3


source to share


1 answer


This code contains a common problem. This is where you create a binding Image

to the stream that contains the bitmap ...

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

      

... and the block using

provides an instance MemoryStream

on exit, which makes it Image

rather useless.



You can get Image

one that manages its own memory rather than expecting your thread to depend 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 is not tied to the original stream.

+3


source







All Articles