不安全代码给出错误 - “不安全代码只有在使用/ unsafe”进行编译时才会出现。 [英] Unsafe Code giving error-"Unsafe code may only appear if compiling with /unsafe"
本文介绍了不安全代码给出错误 - “不安全代码只有在使用/ unsafe”进行编译时才会出现。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在C#中实现图像反转滤镜。
为此,我在C#中使用指针,但在行号中出现错误。 20这是错误只有在使用/ unsafe进行编译时才会出现不安全的代码。
所以我无法删除此错误。
请帮帮我。
提前致谢。
I am trying to implement image invert filter in C#.
For that I am using pointer in C# but there occur an error in line no. 20 which is Error "Unsafe code may only appear if compiling with /unsafe".
So I am unable to remove this error.
Please help me.
Thanks in advance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace DIP
{
class BitmapFilter
{
public static bool Invert(Bitmap b)
{
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width * 3;
int nWidth = b.Width * 3;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
p[0] = (byte)(1000 + p[0]);
++p;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
}
}
推荐答案
您必须选中允许不安全代码复选框在项目的属性中。
You have to check the "Allow unsafe code" checkbox in the project's properties.
这篇关于不安全代码给出错误 - “不安全代码只有在使用/ unsafe”进行编译时才会出现。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文