Pergunta

Hi i try to use some code in my project , a error rise to syntax and i dont know what is it . error come from the tag started with "unsafe" .

What is unsafe and Where and Why Should Use It ? tnx friends...

the code is here :

public Bitmap Cursor(ref int cursorX, ref int cursorY)
        {
            int screenWidth = 1;
            int screenHeight = 1;
            lock (_newBitmap)
            {
                try
                {
                    screenWidth = _newBitmap.Width;
                    screenHeight = _newBitmap.Height;
                }
                catch (Exception)
                {
                    ...
                }
            }
            if (screenWidth == 1 && screenHeight == 1)
            {
                return null;
            }
            Bitmap img = CaptureScreen.CaptureCursor(ref cursorX, ref cursorY);
            if (img != null && cursorX < screenWidth && cursorY < screenHeight)
            {
                int width = img.Width;
                int height = img.Height;

                BitmapData imgData = img.LockBits(
                    new Rectangle(0, 0, width, height),
                    ImageLockMode.ReadOnly, img.PixelFormat);

                const int numBytesPerPixel = 4;

                int stride = imgData.Stride;
                IntPtr scan0 = imgData.Scan0;
                unsafe      // ERROR IN THIS LINE
   //  IF i move unsafe to method header (unsafe public .... ) i get some another errors 
                    {
                        byte* pByte = (byte*)(void*)scan0;
                        for (int h = 0; h < height; h++)
                        {
                            for (int w = 0; w < width; w++)
                            {
                                int offset = h * stride + w * numBytesPerPixel + 3;
                                if (*(pByte + offset) == 0)
                                {
                                    *(pByte + offset) = 60;
                                }
                            }
                        }
                    } 
                    img.UnlockBits(imgData);

                    return img;
                }
                return null;
            }
Foi útil?

Solução 2

i don't khow way you want use of unsafe code but for using this code you must first checked "Allow Unsafe" in project build configuration .

Outras dicas

http://msdn.microsoft.com/en-us/library/chfa2zb8.aspx

The unsafe keyword allows C# code to manipulate pointers. Note that you have to set a project setting to allow the compiler to compile unsafe code; I think it's the /unsafe option.

Your code is manipulating bitmap bits by directly accessing the bitmap in memory. In my experience, this is one of the most common uses of unsafe in C#. Alternatives involving Bitmap.SetPixel are much, much slower.

unsafe allows for you to do raw memory manipulation with pointers. To compile unsafe code you need to tell your project that unsafe code is allowed. Right click on the project and click on build and find the allow unsafe code checkbox. Look for the checkbox.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top