unsafe
                    {
                        Size s = getVideoSize(mediaType);
                        int bmpinfoheaderSize = 40; //equals to sizeof(CommonClasses.BITMAPINFOHEADER);                        //get size for buffer
                        int bufferSize = (((s.Width * s.Height) * 24) / 8) + bmpinfoheaderSize; //equals to mediaDet.GetBitmapBits(0d, ref bufferSize, ref *buffer, target.Width, target.Height);                         //allocates enough memory to store the frame
                        IntPtr frameBuffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(bufferSize);
                        byte* frameBuffer2 = (byte*)frameBuffer.ToPointer();                        //gets bitmap, save in frameBuffer2
                        mediaDet.GetBitmapBits(streamLength * percentagePosition, ref bufferSize, ref *frameBuffer2, target.Width, target.Height);                        //now in buffer2 we have a BITMAPINFOHEADER structure followed by the DIB bits                        Bitmap bmp = new Bitmap(target.Width, target.Height, target.Width * 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(frameBuffer2 + bmpinfoheaderSize));                        bmp.RotateFlip(RotateFlipType.Rotate180FlipX);
                        System.Runtime.InteropServices.Marshal.FreeHGlobal(frameBuffer);                        return bmp;
                    }忽然报错“不安全代码只会在使用 /unsafe 编译的情况下出现”请问是什么原因?

解决方案 »

  1.   

    /unsafe(启用不安全模式)请参见   
      C#   编译器选项   
      /unsafe   
      备注   
      /unsafe   选项允许编译使用   unsafe   关键字的代码。   
        
      在   Visual   Studio   开发环境中设置此编译器选项     
        
      打开此项目的“属性页”对话框。有关详细信息,请参见设置   Visual   C#   项目属性。     
      单击“配置属性”文件夹。     
      单击“生成”属性页。     
      修改“允许不安全代码块”属性。
      

  2.   

    byte* frameBuffer2 = (byte*)frameBuffer.ToPointer();这里使用了指针C#里的指针是非托管代码 属于不安全代码 所以要在项目属性里先开启Allow Unsaft Code选择才能用