简体   繁体   English

使用MemoryStream时参数无效

[英]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). 我正在用C#开发一些位图图像处理应用程序,因此我需要将图像隐式转换为bmp,以便可以像byte [](字节数组)一样对其进行操作。 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[]. 当此图像不是bmp格式时会出现问题-在这种情况下,我将其转换为Bitmap并将此Bitmap转换为byte []并应用了某些功能(通过我“避免”了前53个字节-标题字节)应用中断,尝试在此byte []上使用MemoryStream实例化新的位图时,出现“参数无效”消息。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM