user3647695
user3647695

Reputation: 1

Parameter is not valid when using MemoryStream

I'm making some Bitmap image manipulation application in c# so I need to implicitly convert image to bmp so I can manipulate it like byte[] (byte array). Problem appears when this image is not bmp format - in that case after I convert it to Bitmap and after I convert this Bitmap into byte[] and apply some functions (by the way I "avoided" first 53 bytes - the header bytes) app breaks and I get "Parameter is not valid" message while trying to instance new Bitmap using MemoryStream on this byte[].

This is the code:

public void load(PictureBox pom)
{
    OpenFileDialog o = new OpenFileDialog();

    //o.Filter = "bin files (*.bin)|*.bin";

    if (o.ShowDialog() == DialogResult.OK)
    {
        Bitmap b = (Bitmap)Image.FromFile(o.FileName);

        pom.Image = b;

        //pom.Image = new Bitmap(o.FileName);
        this.p = pom;
        this.input_bin_fajl = File.ReadAllBytes(o.FileName);
        this.output_bin_fajl = new byte[this.input_bin_fajl.Length];
    }
}

public static Bitmap ByteToImage(byte[] blob)
{
    using (MemoryStream mStream = new MemoryStream())
    {
        mStream.Write(blob, 0, blob.Length);
        mStream.Seek(0, SeekOrigin.Begin);

        // this is the breaking point
        Bitmap bm = new Bitmap(mStream);
        //

        return bm;
    }
}

Upvotes: 0

Views: 1156

Answers (1)

user3647695
user3647695

Reputation: 1

Here is the code it works with unsafe thanks again :)

    void preview(Bitmap bm, int i)
    {
        BitmapData bmData = bm.LockBits(new Rectangle(0,0,bm.Width,bm.Height), ImageLockMode.ReadWrite, bm.PixelFormat);
        int stride = bmData.Stride;
        System.IntPtr Scan0 = bmData.Scan0;

        unsafe
        {
            byte* p = (byte*)(void*)Scan0;
            int nOffset = stride - bm.Width * 3;
            int nWidth = bm.Width * 3;

            for (int y = 0; y < bm.Height; ++y)
            {
                for (int x = 0; x < bm.Width; ++x)
                {
                    switch (i)
                    {
                        case 0:
                            {
                                p[0] = (byte)0;
                                p[1] = (byte)0;
                                break;
                            }
                        case 1:
                            {
                                p[0] = (byte)0;
                                p[2] = (byte)0;
                                break;
                            }
                        default:
                            {
                                p[1] = (byte)0;
                                p[2] = (byte)0;
                                break;
                            }                               
                    }
                    p+=3;
                }
                p += nOffset;
            }
        }

        bm.UnlockBits(bmData);
    }

Upvotes: 0

Related Questions