src/Foundation/Media/Gimela.Media.Imaging/Helpers/DrawingHelper.cs
/// <summary>
/// Draw rectangle on the specified image.
/// </summary>
/// 
/// <param name="image">Source image to draw on.</param>
/// <param name="rectangle">Rectangle's coordinates to draw.</param>
/// <param name="color">Rectangle's color.</param>
/// 
/// <exception cref="UnsupportedImageFormatException">The source image has incorrect pixel format.</exception>
/// 
Draw rectangle on the specified image.
Draw rectangle on the specified image .
public static unsafe void Rectangle(UnmanagedImage image, Rectangle rectangle, Color color)
    {
      CheckPixelFormat(image.PixelFormat);
      int pixelSize = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
      int imageWidth = image.Width;
      int imageHeight = image.Height;
      int stride = image.Stride;
      int rectX1 = rectangle.X;
      int rectY1 = rectangle.Y;
      int rectX2 = rectangle.X + rectangle.Width - 1;
      int rectY2 = rectangle.Y + rectangle.Height - 1;
      if ((rectX1 >= imageWidth) || (rectY1 >= imageHeight) || (rectX2 < 0) || (rectY2 < 0))
      {
        return;
      }
      int startX = Math.Max(0, rectX1);
      int stopX = Math.Min(imageWidth - 1, rectX2);
      int startY = Math.Max(0, rectY1);
      int stopY = Math.Min(imageHeight - 1, rectY2);
      if (image.PixelFormat == PixelFormat.Format8bppIndexed)
      {
        byte gray = (byte)(0.2125 * color.R + 0.7154 * color.G + 0.0721 * color.B);
        if (rectY1 >= 0)
        {
          byte* ptr = (byte*)image.ImageData.ToPointer() + rectY1 * stride + startX;
          UnmanagedMemoryHelper.SetUnmanagedMemory(ptr, gray, stopX - startX);
        }
        if (rectY2 < imageHeight)
        {
          byte* ptr = (byte*)image.ImageData.ToPointer() + rectY2 * stride + startX;
          UnmanagedMemoryHelper.SetUnmanagedMemory(ptr, gray, stopX - startX);
        }
        if (rectX1 >= 0)
        {
          byte* ptr = (byte*)image.ImageData.ToPointer() + startY * stride + rectX1;
          for (int y = startY; y <= stopY; y++, ptr += stride)
          {
            *ptr = gray;
          }
        }
        if (rectX2 < imageWidth)
        {
          byte* ptr = (byte*)image.ImageData.ToPointer() + startY * stride + rectX2;
          for (int y = startY; y <= stopY; y++, ptr += stride)
          {
            *ptr = gray;
          }
        }
      }
      else
      {
        byte red = color.R;
        byte green = color.G;
        byte blue = color.B;
        if (rectY1 >= 0)
        {
          byte* ptr = (byte*)image.ImageData.ToPointer() + rectY1 * stride + startX * pixelSize;
          for (int x = startX; x <= stopX; x++, ptr += pixelSize)
          {
            ptr[RGB.R] = red;
            ptr[RGB.G] = green;
            ptr[RGB.B] = blue;
          }
        }
        if (rectY2 < imageHeight)
        {
          byte* ptr = (byte*)image.ImageData.ToPointer() + rectY2 * stride + startX * pixelSize;
          for (int x = startX; x <= stopX; x++, ptr += pixelSize)
          {
            ptr[RGB.R] = red;
            ptr[RGB.G] = green;
            ptr[RGB.B] = blue;
          }
        }
        if (rectX1 >= 0)
        {
          byte* ptr = (byte*)image.ImageData.ToPointer() + startY * stride + rectX1 * pixelSize;
          for (int y = startY; y <= stopY; y++, ptr += stride)
          {
            ptr[RGB.R] = red;
            ptr[RGB.G] = green;
            ptr[RGB.B] = blue;
          }
        }
        if (rectX2 < imageWidth)
        {
          byte* ptr = (byte*)image.ImageData.ToPointer() + startY * stride + rectX2 * pixelSize;
          for (int y = startY; y <= stopY; y++, ptr += stride)
          {
            ptr[RGB.R] = red;
            ptr[RGB.G] = green;
            ptr[RGB.B] = blue;
          }
        }
      }
    }
public static unsafe void Rectangle ( UnmanagedImage image , Rectangle rectangle , Color color ) { CheckPixelFormat ( image . PixelFormat ) ; int pixelSize = System . Drawing . Image . GetPixelFormatSize ( image . PixelFormat ) / 8 ; int imageWidth = image . Width ; int imageHeight = image . Height ; int stride = image . Stride ; int rectX1 = rectangle . X ; int rectY1 = rectangle . Y ; int rectX2 = rectangle . X + rectangle . Width - 1 ; int rectY2 = rectangle . Y + rectangle . Height - 1 ; if ( ( rectX1 >= imageWidth ) || ( rectY1 >= imageHeight ) || ( rectX2 < 0 ) || ( rectY2 < 0 ) ) { return ; } int startX = Math . Max ( 0 , rectX1 ) ; int stopX = Math . Min ( imageWidth - 1 , rectX2 ) ; int startY = Math . Max ( 0 , rectY1 ) ; int stopY = Math . Min ( imageHeight - 1 , rectY2 ) ; if ( image . PixelFormat == PixelFormat . Format8bppIndexed ) { byte gray = ( byte ) ( 0.2125 * color . R + 0.7154 * color . G + 0.0721 * color . B ) ; if ( rectY1 >= 0 ) { byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + rectY1 * stride + startX ; UnmanagedMemoryHelper . SetUnmanagedMemory ( ptr , gray , stopX - startX ) ; } if ( rectY2 < imageHeight ) { byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + rectY2 * stride + startX ; UnmanagedMemoryHelper . SetUnmanagedMemory ( ptr , gray , stopX - startX ) ; } if ( rectX1 >= 0 ) { byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + startY * stride + rectX1 ; for ( int y = startY ; y <= stopY ; y ++ , ptr += stride ) { * ptr = gray ; } } if ( rectX2 < imageWidth ) { byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + startY * stride + rectX2 ; for ( int y = startY ; y <= stopY ; y ++ , ptr += stride ) { * ptr = gray ; } } } else { byte red = color . R ; byte green = color . G ; byte blue = color . B ; if ( rectY1 >= 0 ) { byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + rectY1 * stride + startX * pixelSize ; for ( int x = startX ; x <= stopX ; x ++ , ptr += pixelSize ) { ptr [ RGB . R ] = red ; ptr [ RGB . G ] = green ; ptr [ RGB . B ] = blue ; } } if ( rectY2 < imageHeight ) { byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + rectY2 * stride + startX * pixelSize ; for ( int x = startX ; x <= stopX ; x ++ , ptr += pixelSize ) { ptr [ RGB . R ] = red ; ptr [ RGB . G ] = green ; ptr [ RGB . B ] = blue ; } } if ( rectX1 >= 0 ) { byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + startY * stride + rectX1 * pixelSize ; for ( int y = startY ; y <= stopY ; y ++ , ptr += stride ) { ptr [ RGB . R ] = red ; ptr [ RGB . G ] = green ; ptr [ RGB . B ] = blue ; } } if ( rectX2 < imageWidth ) { byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + startY * stride + rectX2 * pixelSize ; for ( int y = startY ; y <= stopY ; y ++ , ptr += stride ) { ptr [ RGB . R ] = red ; ptr [ RGB . G ] = green ; ptr [ RGB . B ] = blue ; } } } }
Draw rectangle on the specified image.
Draw rectangle on the specified image .
// image dimension // rectangle dimension and position // check if rectangle is in the image // nothing to draw // grayscale image // draw top horizontal line // draw bottom horizontal line // draw left vertical line // draw right vertical line // color image // draw top horizontal line // draw bottom horizontal line // draw left vertical line // draw right vertical line
The source image has incorrect pixel format.
The source image has incorrect pixel format .
image
UnmanagedImage
Source image to draw on.
Source image to draw on .
rectangle
Rectangle
Rectangle's coordinates to draw.
Rectangle ' s coordinates to draw .
color
Color
None