src/Foundation/Media/Gimela.Media.Imaging/Helpers/ImageHelper.cs
/// <summary>
/// Convert bitmap with 16 bits per plane to a bitmap with 8 bits per plane.
/// </summary>
/// 
/// <param name="bimap">Source image to convert.</param>
/// 
/// <returns>Returns new image which is a copy of the source image but with 8 bits per plane.</returns>
/// 
/// <remarks><para>The routine does the next pixel format conversions:
/// <list type="bullet">
/// <item><see cref="PixelFormat.Format16bppGrayScale">Format16bppGrayScale</see> to
/// <see cref="PixelFormat.Format8bppIndexed">Format8bppIndexed</see> with grayscale palette;</item>
/// <item><see cref="PixelFormat.Format48bppRgb">Format48bppRgb</see> to
/// <see cref="PixelFormat.Format24bppRgb">Format24bppRgb</see>;</item>
/// <item><see cref="PixelFormat.Format64bppArgb">Format64bppArgb</see> to
/// <see cref="PixelFormat.Format32bppArgb">Format32bppArgb</see>;</item>
/// <item><see cref="PixelFormat.Format64bppPArgb">Format64bppPArgb</see> to
/// <see cref="PixelFormat.Format32bppPArgb">Format32bppPArgb</see>.</item>
/// </list>
/// </para></remarks>
/// 
/// <exception cref="UnsupportedImageFormatException">Invalid pixel format of the source image.</exception>
/// 
Convert bitmap with 16 bits per plane to a bitmap with 8 bits per plane.
Convert bitmap with 16 bits per plane to a bitmap with 8 bits per plane .
public static Bitmap Convert16bppTo8bpp(Bitmap bimap)
    {
      Bitmap newImage = null;
      int layers = 0;
      int width = bimap.Width;
      int height = bimap.Height;
      switch (bimap.PixelFormat)
      {
        case PixelFormat.Format16bppGrayScale:
          newImage = CreateGrayscaleImage(width, height);
          layers = 1;
          break;
        case PixelFormat.Format48bppRgb:
          newImage = new Bitmap(width, height, PixelFormat.Format24bppRgb);
          layers = 3;
          break;
        case PixelFormat.Format64bppArgb:
          newImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
          layers = 4;
          break;
        case PixelFormat.Format64bppPArgb:
          newImage = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
          layers = 4;
          break;
        default:
          throw new UnsupportedImageFormatException("Invalid pixel format of the source image.");
      }
      BitmapData sourceData = bimap.LockBits(new Rectangle(0, 0, width, height),
          ImageLockMode.ReadOnly, bimap.PixelFormat);
      BitmapData newData = newImage.LockBits(new Rectangle(0, 0, width, height),
          ImageLockMode.ReadWrite, newImage.PixelFormat);
      unsafe
      {
        byte* sourceBasePtr = (byte*)sourceData.Scan0.ToPointer();
        byte* newBasePtr = (byte*)newData.Scan0.ToPointer();
        int sourceStride = sourceData.Stride;
        int newStride = newData.Stride;
        for (int y = 0; y < height; y++)
        {
          ushort* sourcePtr = (ushort*)(sourceBasePtr + y * sourceStride);
          byte* newPtr = (byte*)(newBasePtr + y * newStride);
          for (int x = 0, lineSize = width * layers; x < lineSize; x++, sourcePtr++, newPtr++)
          {
            *newPtr = (byte)(*sourcePtr >> 8);
          }
        }
      }
      bimap.UnlockBits(sourceData);
      newImage.UnlockBits(newData);
      return newImage;
    }
public static Bitmap Convert16bppTo8bpp ( Bitmap bimap ) { Bitmap newImage = null ; int layers = 0 ; int width = bimap . Width ; int height = bimap . Height ; switch ( bimap . PixelFormat ) { case PixelFormat . Format16bppGrayScale : newImage = CreateGrayscaleImage ( width , height ) ; layers = 1 ; break ; case PixelFormat . Format48bppRgb : newImage = new Bitmap ( width , height , PixelFormat . Format24bppRgb ) ; layers = 3 ; break ; case PixelFormat . Format64bppArgb : newImage = new Bitmap ( width , height , PixelFormat . Format32bppArgb ) ; layers = 4 ; break ; case PixelFormat . Format64bppPArgb : newImage = new Bitmap ( width , height , PixelFormat . Format32bppPArgb ) ; layers = 4 ; break ; default : throw new UnsupportedImageFormatException ( " " ) ; } BitmapData sourceData = bimap . LockBits ( new Rectangle ( 0 , 0 , width , height ) , ImageLockMode . ReadOnly , bimap . PixelFormat ) ; BitmapData newData = newImage . LockBits ( new Rectangle ( 0 , 0 , width , height ) , ImageLockMode . ReadWrite , newImage . PixelFormat ) ; unsafe { byte * sourceBasePtr = ( byte * ) sourceData . Scan0 . ToPointer ( ) ; byte * newBasePtr = ( byte * ) newData . Scan0 . ToPointer ( ) ; int sourceStride = sourceData . Stride ; int newStride = newData . Stride ; for ( int y = 0 ; y < height ; y ++ ) { ushort * sourcePtr = ( ushort * ) ( sourceBasePtr + y * sourceStride ) ; byte * newPtr = ( byte * ) ( newBasePtr + y * newStride ) ; for ( int x = 0 , lineSize = width * layers ; x < lineSize ; x ++ , sourcePtr ++ , newPtr ++ ) { * newPtr = ( byte ) ( * sourcePtr >> 8 ) ; } } } bimap . UnlockBits ( sourceData ) ; newImage . UnlockBits ( newData ) ; return newImage ; }
Convert bitmap with 16 bits per plane to a bitmap with 8 bits per plane.
Convert bitmap with 16 bits per plane to a bitmap with 8 bits per plane .
// get image size // create new image depending on source image format // create new grayscale image // create new color 24 bpp image // create new color 32 bpp image // create new color 32 bpp image // lock both images // base pointers // image strides // unlock both image
Returns new image which is a copy of the source image but with 8 bits per plane.
Returns new image which is a copy of the source image but with 8 bits per plane .
Invalid pixel format of the source image.
Invalid pixel format of the source image .
bimap
Bitmap
Source image to convert.
Source image to convert .
remarks
The routine does the next pixel format conversions:
Format16bppGrayScaleFormat8bppIndexedFormat48bppRgbFormat24bppRgbFormat64bppArgbFormat32bppArgbFormat64bppPArgbFormat32bppPArgb
The routine does the next pixel format conversions : Format16bppGrayScaleFormat8bppIndexedFormat48bppRgbFormat24bppRgbFormat64bppArgbFormat32bppArgbFormat64bppPArgbFormat32bppPArgb