SoFunction
Updated on 2025-03-08

Methods for linear transformation of C# images

This article describes the method of linear transformation of C# images. Share it for your reference. The details are as follows:

//Define the image linear operation function (y=kx+v)private static Bitmap LinearOP(Bitmap a, double k, double v)
{
  Rectangle rect = new Rectangle(0, 0, , );
   srcData = (rect, , );
  IntPtr ptr = srcData.Scan0;
  int bytes = 0;
  bytes =  * ;
  byte[] grayValues = new byte[bytes];
  (ptr, grayValues, 0, bytes);
  int temp = 0;
  for (int i = 0; i < bytes; i++)
  {
   temp = (int)(k * grayValues[i] + v + 0.5);
   temp = (temp > 255) ? 255 : temp < 0 ? 0 : temp;
   grayValues[i] = (byte)temp;
  }
  (grayValues, 0, ptr, bytes);
  (srcData);
  return a;
}

I hope this article will be helpful to everyone's C# programming.