Click here to Skip to main content
15,937,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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;
        }
    }
}
Posted
Updated 13-Feb-18 22:19pm
v2

1 solution

You have to check the "Allow unsafe code" checkbox in the project's properties.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900