Search code examples
c#.netimagebitmapphotoshop

Implementing Photoshop filters in C#


I know how to implement them, but what I don't know is whether to apply the transformation pixel by pixel or is there another way to affect the whole image, using a single call, etc?

AFAIK Get.Set Pixel is very slow. I am not sure if they did it this way.

So if it's the grayscale/desaturate filter as a simple case, how would one write it?


Solution

  • You have to lock the image and then work with the memory, directly bypassing SetPixel method. See here or even better here.

    For examle you can set the blue channel to 255 as follows:

       BitmapData bmd=bm.LockBits(new Rectangle(0, 0, 10, 10), System.Drawing.Imaging.ImageLockMode.ReadOnly, bm.PixelFormat);
          int PixelSize=4;
          for(int y=0; y<bmd.Height; y++)
          {
            byte* row=(byte *)bmd.Scan0+(y*bmd.Stride);
            for(int x=0; x<bmd.Width; x++)
            {
              row[x*PixelSize]=255;
            }
          } // it is copied from the last provided link.