0

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;
            }
user3290286
  • 687
  • 1
  • 7
  • 8
  • 1
    And can you provide the error message? – Pierre-Luc Pineault Feb 20 '14 at 21:05
  • 6
    using `unsafe` without knowing its purpose sounds...unsafe... – Jonesopolis Feb 20 '14 at 21:06
  • 2
    You prob. dont have unsafe enabled, its a compiler switch: http://msdn.microsoft.com/en-us/library/chfa2zb8.aspx – iamkrillin Feb 20 '14 at 21:07
  • 1
    @Jonesy I SAID i use a sample source and its Contains "unsafe" Keyword . i dont understood it and i think write ask it in StackOverFlow for i learn it . i dont know this Negative score is for what . its my mistake . I thought here answering to my Question , its my mistake . anyway thank you. – user3290286 Feb 20 '14 at 21:32

3 Answers3

2

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.

NathanAldenSr
  • 7,841
  • 4
  • 40
  • 51
1

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.

rerun
  • 25,014
  • 6
  • 48
  • 78
1

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 .