[Tutorial] How to negative a bitmap in C# with low-level API

Example requests & Code samples for GdPicture Toolkits.
Post Reply
User avatar
Loïc
Site Admin
Posts: 5662
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

[Tutorial] How to negative a bitmap in C# with low-level API

Post by Loïc » Fri May 06, 2011 4:28 pm

Hi,

Here a C# sample that demonstrates how to use the low-level API of GdPicture to access bits of a Bitmap.

The sample prompts the user to select a bitmap, apply a negative effect, and save the processed bitmap into c:\negative.tif

Supported bitdepth are 1bpp, 4bpp (grayscale), 8bpp (grayscale), 16bpp, 24bpp & 32bpp.


Feel free to ask any technical question.

Code: Select all

      unsafe private void negativeImage(){
         GdPictureImaging oGdPictureImaging = new GdPictureImaging();
         oGdPictureImaging.SetLicenseNumber("XXX");

         int ImageID = oGdPictureImaging.CreateGdPictureImageFromFile(""); //Prompt the user to select a file, and try to open it as a memory Bitmap
         if (ImageID != 0)
         {
            int bpp = oGdPictureImaging.GetBitDepth(ImageID); //Number of bit per pixel.
            byte* pData = (byte*)oGdPictureImaging.GetBits(ImageID);
            int width = oGdPictureImaging.GetWidth(ImageID); 
            int height = oGdPictureImaging.GetHeight(ImageID);
            int stride = oGdPictureImaging.GetStride(ImageID);
            
            switch (bpp){
               case 1:
               case 4: //We should check the image uses grayscale palette entry for this configuration, else the color palette should be modified
               case 8: //same remark as prev.
               case 16:
               case 24:
                  {
                     for (int y = 0; y < height; y++)
                     {
                        byte* pScanline = pData;
                        for (int x = 0; x < stride; x++)
                        {
                           pScanline[x] = (byte)(0xFF - pScanline[x]);
                        }
                        pData += stride;//increment pointer to next scanline
                     }
                      break;
                  }
               case 32:
                  {
                     for (int y = 0; y < height; y++)
                     {
                        for (int x = 0; x < width; x++)
                        {
                           //negative Blue component
                           *pData = (byte)(0xFF - *pData);
                           pData++;
                           //negative Green component
                           *pData = (byte)(0xFF - *pData);
                           pData++;
                           //negative Red component
                           *pData = (byte)(0xFF - *pData);
                           pData++;
                           //squizz alpha/reserved component
                           pData++;
                        }
                     }
                    
                     break;
                  }
               default:
                  throw new Exception("Pixel format not supported"); 
            }
            oGdPictureImaging.SaveAsTIFF(ImageID, "c:\\negative.tif", TiffCompression.TiffCompressionAUTO);
            oGdPictureImaging.ReleaseGdPictureImage(ImageID);
 
         }
      }
   }

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest