不安全代码给出错误

编程入门 行业动态 更新时间:2024-10-10 06:19:10
本文介绍了不安全代码给出错误 - “不安全代码只有在使用/ 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.

更多推荐

不安全代码给出错误

本文发布于:2023-11-15 06:29:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591968.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不安全   错误   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!