3

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.

4
  • And how do you save the images? Commented Jul 8, 2015 at 21:01
  • 5
    Please show the complete exception along with a stack trace, as well as the code that is throwing the exception. Commented Jul 8, 2015 at 21:01
  • 1
    As others noted the code for "save out a picture from my other program and try to open it in the test program" would be useful to see. Commented Jul 8, 2015 at 21:02
  • edited. That's the whole thing. My big program is saving images exactly the same way. Commented Jul 8, 2015 at 21:16

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Good to know and thank you for the info. But the problem persists in the same manner. I can open one file but not one that is exactly the same but created from my other program. My other program also adds some extra info above the image which I must manually delete, but then the files are exactly the same. I save and then cannot read it in my test program.
So I decided to copy the text from the working file and paste it into the other non-working file, just to make sure they were identical and it still didn't work. Then when I pasted the working file text back into itself after deleting its contents it failed to open and threw the same error. What is it about being modified that causes this? Even when the modification is just deleting the text and repasting the same text?
Text? Bitmap files need tools designed for processing binary data.
The images are being converted to byte arrays then saved to a text file. Which works, but not consistently. Is this problem caused by encoding? Or lack thereof?
Perhaps. You should at least use binary tools (eg hexadecimal view of bytes) to determine what is different between your working and non-working cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.