Longkong.ColorPicker.Controls.ColorSwatchPanel.BuildSwatchBitmap C# (CSharp) Method

BuildSwatchBitmap() private method

private BuildSwatchBitmap ( ) : Bitmap
return System.Drawing.Bitmap
        private Bitmap BuildSwatchBitmap()
        {
            int x = m_startX;
            int y = m_startY;
            bool isFull = false;

            int swatchColumnNum = 0;
            int swatchRowNum = 0;

            ColorSwatch[] swatches = ( ColorSwatch[] ) m_swatches.ToArray( typeof( ColorSwatch ) );

            Bitmap swatchBitmap = new Bitmap( m_swatchOuterRegionWidth, m_swatchOuterRegionHeight );

            using ( Graphics g = Graphics.FromImage( swatchBitmap ) ) {

                for ( int i=0; i < swatches.Length; i++ ) {

                    swatches[i].Location = new Point( x, y );
                    swatches[i].Size = new Size( SWATCH_WIDTH, SWATCH_HEIGHT );

                    m_swatchArray[ swatchColumnNum++, swatchRowNum ] = swatches[ i ];

                    using ( SolidBrush sb = new SolidBrush( swatches[i].Color ) ) {
                        g.FillRectangle( sb, x, y, 10, 10 );
                    }

                    g.DrawRectangle( Pens.Black, x, y, 10, 10 );

                    UpdatePositions( ref x, ref y, ref swatchColumnNum, ref swatchRowNum );

                    if ( y + SWATCH_HEIGHT > m_swatchOuterRegionHeight ) {
                        isFull = true;
                        break;
                    }

                    m_totalNumberOfSwatches++;

                }

                m_swatchRowNum = swatchRowNum;
                m_swatchColumnNum = swatchColumnNum;

                if ( !isFull ) {

                    m_numberOfEmptySwatches = 0;

                    while ( true ) {

                        if ( ++m_numberOfEmptySwatches == 1 ) {
                            m_nextEmptySwatchPoint = new Point( x, y );
                        }

                        g.DrawRectangle( Pens.DarkGray, x, y, 10, 10 );
                        UpdatePositions( ref x, ref y, ref swatchColumnNum, ref swatchRowNum );

                        if ( y + SWATCH_HEIGHT > m_swatchOuterRegionHeight ) {
                            isFull = true;
                            break;
                        }

                        m_totalNumberOfSwatches++;

                    }

                }

            }

            return swatchBitmap;
        }