C# C语言中的不安全错误#

C# C语言中的不安全错误#,c#,unsafe,C#,Unsafe,嗨,我尝试在我的项目中使用一些代码,语法出现了错误,我不知道是什么。 错误来自以“不安全”开头的标记 什么是不安全的,在哪里使用,为什么使用? tnx朋友 代码如下: public Bitmap Cursor(ref int cursorX, ref int cursorY) { int screenWidth = 1; int screenHeight = 1; lock (_newBitmap)

嗨,我尝试在我的项目中使用一些代码,语法出现了错误,我不知道是什么。 错误来自以“不安全”开头的标记

什么是不安全的,在哪里使用,为什么使用? tnx朋友

代码如下:

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;
            }
公共位图光标(ref int cursorX,ref int cursorY)
{
int屏幕宽度=1;
int屏幕高度=1;
锁(_newBitmap)
{
尝试
{
屏幕宽度=_newBitmap.Width;
屏幕高度=_newBitmap.Height;
}
捕获(例外)
{
...
}
}
如果(屏幕宽度==1&&screenHeight==1)
{
返回null;
}
位图img=CaptureScreen.CaptureCursor(参考游标x,参考游标);
if(img!=null&&cursorX

unsafe
关键字允许C#code操作指针。请注意,您必须设置项目设置以允许编译器编译不安全的代码;我认为这是
/不安全的
选项

代码通过直接访问内存中的位图来操作位图位。根据我的经验,这是C#中
unsafe
最常见的用法之一。涉及的替代方案要慢得多。

unsafe
关键字允许C#code操作指针。请注意,您必须设置项目设置以允许编译器编译不安全的代码;我认为这是
/不安全的
选项


代码通过直接访问内存中的位图来操作位图位。根据我的经验,这是C#中
unsafe
最常见的用法之一。涉及的替代方案要慢得多。

不安全允许您使用指针进行原始内存操作。要编译不安全代码,您需要告诉您的项目允许使用不安全代码。在项目上单击鼠标右键,然后单击“生成”并找到“允许不安全代码”复选框

不安全允许您使用指针执行原始内存操作。要编译不安全代码,您需要告诉您的项目允许使用不安全代码。在项目上单击鼠标右键,然后单击“生成”并找到“允许不安全代码”复选框

我不知道您希望使用不安全代码的方式,但要使用此代码,必须首先在项目构建配置中选中“允许不安全”

我不知道您希望使用不安全代码的方式,但要使用此代码,必须首先在项目构建配置中选中“允许不安全”

你能提供错误信息吗?在不知道其目的的情况下使用
safe
,听起来…不安全…你有问题。不要启用不安全,这是一个编译器开关:@Jonesy我说我使用了一个示例源,它包含“不安全”关键字。我不懂它,我想写下来,问一下,因为我学会了。我不知道这个负分是为了什么。这是我的错。我想在这里回答我的问题,这是我的错误。无论如何,谢谢。你能提供错误消息吗?在不知道其目的的情况下使用
不安全
,听起来…不安全…你可能会。不要启用不安全,这是一个编译器开关:@Jonesy我说我使用了一个示例源,它包含“不安全”关键字。我不懂它,我想写下来,问一下,因为我学会了。我不知道这个负分是为了什么。这是我的错。我想在这里回答我的问题,这是我的错误。无论如何,谢谢你。