簡體   English   中英

使用MemoryStream時參數無效

[英]Parameter is not valid when using MemoryStream

我正在用C#開發一些位圖圖像處理應用程序,因此我需要將圖像隱式轉換為bmp,以便可以像byte [](字節數組)一樣對其進行操作。 當此圖像不是bmp格式時會出現問題-在這種情況下,我將其轉換為Bitmap並將此Bitmap轉換為byte []並應用了某些功能(通過我“避免”了前53個字節-標題字節)應用中斷,嘗試在此byte []上使用MemoryStream實例化新的位圖時,出現“參數無效”消息。

這是代碼:

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

這是它不安全的代碼,再次感謝:)

    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