첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

#region 메모리 설정하기 - memset(targetPointer, fillerValue, count)

/// <summary>
/// 메모리 설정하기
/// </summary>
/// <param name="targetPointer">목표 주소</param>
/// <param name="fillerValue">채우기 값</param>
/// <param name="count">수</param>
/// <returns>목표 주소</returns>
[DllImport("ntdll.dll")]
public static extern unsafe byte * memset(byte* targetPointer, int fillerValue, int count);

#endregion

#region 사각형 칠하기 - FillRectangle(sourceBitmapData, rectangle, color)

/// <summary>
/// 사각형 칠하기
/// </summary>
/// <param name="sourceBitmapData">소스 비트맵 데이타</param>
/// <param name="rectangle">사각형</param>
/// <param name="color">색상</param>
/// <remarks>소스 비트맵 데이타 픽셀 형식은 24비트 색상 또는 256 인덱스 색상이어야 합니다.</remarks>
public void FillRectangle(BitmapData sourceBitmapData, Rectangle rectangle, Color color)
{
    int bitmapWidth  = sourceBitmapData.Width;
    int bitmapHeight = sourceBitmapData.Height;
    int stride       = sourceBitmapData.Stride;

    int rectangleX1 = rectangle.X;
    int rectangleY1 = rectangle.Y;
    int rectangleX2 = rectangle.X + rectangle.Width  - 1;
    int rectangleY2 = rectangle.Y + rectangle.Height - 1;

    if((rectangleX1 >= bitmapWidth) || (rectangleY1 >= bitmapHeight) || (rectangleX2 < 0) || (rectangleY2 < 0))
    {
        return;
    }

    int startX = Math.Max(0, rectangleX1);
    int endX   = Math.Min(bitmapWidth - 1, rectangleX2);
    int startY = Math.Max(0, rectangleY1);
    int endY   = Math.Min(bitmapHeight - 1, rectangleY2);

    unsafe
    {
        byte* sourcePointer = (byte*)sourceBitmapData.Scan0.ToPointer() + startY * stride + startX * ((sourceBitmapData.PixelFormat == PixelFormat.Format8bppIndexed) ? 1 : 3);

        if(sourceBitmapData.PixelFormat == PixelFormat.Format8bppIndexed)
        {
            byte gray = (byte)(0.2125d * color.R + 0.7154d * color.G + 0.0721d * color.B);

            int fillWidth = endX - startX + 1;

            for(int y = startY; y <= endY; y++)
            {
                memset(sourcePointer, gray, fillWidth);

                sourcePointer += stride;
            }
        }
        else
        {
            byte red    = color.R;
            byte green  = color.G;
            byte blue   = color.B;
            int  offset = stride - (endX - startX + 1) * 3;

            for(int y = startY; y <= endY; y++)
            {
                for(int x = startX; x <= endX; x++, sourcePointer += 3)
                {
                    sourcePointer[0] = blue;
                    sourcePointer[1] = green;
                    sourcePointer[2] = red;
                }

                sourcePointer += offset;
            }
        }
    }
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요