我们可以访问C#中的指针吗?

我们可以访问C#中的指针吗?,c#,.net,C#,.net,我只是想知道我们在哪里可以使用C#中的指针 我们可以使用C++中的指针,但我不知道在C语言中我们可以在哪里使用它们。 我还想知道非托管代码中是否使用了指针。是的,您可以使用指针 一个实际使用的示例:将图像转换为灰度 public static Bitmap MakeGrayscale2(Bitmap original) { unsafe { //create an empty bitmap the same size as original Bitmap ne

我只是想知道我们在哪里可以使用C#中的指针

我们可以使用C++中的指针,但我不知道在C语言中我们可以在哪里使用它们。


我还想知道非托管代码中是否使用了指针。

是的,您可以使用指针

一个实际使用的示例:将图像转换为灰度

public static Bitmap MakeGrayscale2(Bitmap original)
{
   unsafe
   {
      //create an empty bitmap the same size as original
      Bitmap newBitmap = new Bitmap(original.Width, original.Height);

      //lock the original bitmap in memory
      BitmapData originalData = original.LockBits(
         new Rectangle(0, 0, original.Width, original.Height),
         ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

      //lock the new bitmap in memory
      BitmapData newData = newBitmap.LockBits(
         new Rectangle(0, 0, original.Width, original.Height), 
         ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

      //set the number of bytes per pixel
      int pixelSize = 3;

      for (int y = 0; y < original.Height; y++)
      {
         //get the data from the original image
         byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);

         //get the data from the new image
         byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);

         for (int x = 0; x < original.Width; x++)
         {
            //create the grayscale version
            byte grayScale = 
               (byte)((oRow[x * pixelSize] * .11) + //B
               (oRow[x * pixelSize + 1] * .59) +  //G
               (oRow[x * pixelSize + 2] * .3)); //R

            //set the new image's pixel to the grayscale version
            nRow[x * pixelSize] = grayScale; //B
            nRow[x * pixelSize + 1] = grayScale; //G
            nRow[x * pixelSize + 2] = grayScale; //R
         }
      }

      //unlock the bitmaps
      newBitmap.UnlockBits(newData);
      original.UnlockBits(originalData);

      return newBitmap;
   }
}
公共静态位图MakeGrayscale2(位图原始)
{
不安全的
{
//创建与原始位图大小相同的空位图
位图newBitmap=新位图(original.Width、original.Height);
//在内存中锁定原始位图
BitmapData originalData=原始.LockBits(
新矩形(0,0,原始宽度,原始高度),
ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);
//在内存中锁定新位图
BitmapData newData=newBitmap.LockBits(
新矩形(0,0,原始宽度,原始高度),
ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);
//设置每个像素的字节数
int pixelSize=3;
对于(int y=0;y

.

您可以在
不安全的
上下文中访问指针。否则你不会:语言会使它变得模糊。你很少(如果有的话)在C#中使用指针。查看为什么要访问指针?你想要达到的目标是什么?没有指针是无法实现的?谢谢,非常好的描述