public static void DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft)
        {
            if (g == null)
            {
                throw new ArgumentNullException(nameof(g));
            }
            if (backgroundImageLayout == ImageLayout.Tile)
            {
                using (TextureBrush textureBrush = new TextureBrush(backgroundImage, WrapMode.Tile)) {
                    if (scrollOffset != Point.Empty)
                    {
                        Matrix transform = textureBrush.Transform;
                        transform.Translate((float)scrollOffset.X, (float)scrollOffset.Y);
                        textureBrush.Transform = transform;
                    }
                    g.FillRectangle(textureBrush, clipRect);
                    return;
                }
            }
            Rectangle rectangle = ControlPaintWrapper.CalculateBackgroundImageRectangle(bounds, backgroundImage, backgroundImageLayout);

            if (rightToLeft == RightToLeft.Yes && backgroundImageLayout == ImageLayout.None)
            {
                rectangle.X += clipRect.Width - rectangle.Width;
            }
            using (SolidBrush solidBrush = new SolidBrush(backColor)) {
                g.FillRectangle(solidBrush, clipRect);
            }
            if (!clipRect.Contains(rectangle))
            {
                if (backgroundImageLayout == ImageLayout.Stretch || backgroundImageLayout == ImageLayout.Zoom)
                {
                    rectangle.Intersect(clipRect);
                    g.DrawImage(backgroundImage, rectangle);
                    return;
                }
                if (backgroundImageLayout == ImageLayout.None)
                {
                    rectangle.Offset(clipRect.Location);
                    Rectangle destRect = rectangle;
                    destRect.Intersect(clipRect);
                    Rectangle rectangle2 = new Rectangle(Point.Empty, destRect.Size);
                    g.DrawImage(backgroundImage, destRect, rectangle2.X, rectangle2.Y, rectangle2.Width, rectangle2.Height, GraphicsUnit.Pixel);
                    return;
                }
                Rectangle destRect2 = rectangle;
                destRect2.Intersect(clipRect);
                Rectangle rectangle3 = new Rectangle(new Point(destRect2.X - rectangle.X, destRect2.Y - rectangle.Y), destRect2.Size);
                g.DrawImage(backgroundImage, destRect2, rectangle3.X, rectangle3.Y, rectangle3.Width, rectangle3.Height, GraphicsUnit.Pixel);
                return;
            }
            else
            {
                ImageAttributes imageAttributes = new ImageAttributes();
                imageAttributes.SetWrapMode(WrapMode.TileFlipXY);
                g.DrawImage(backgroundImage, rectangle, 0, 0, backgroundImage.Width, backgroundImage.Height, GraphicsUnit.Pixel, imageAttributes);
                imageAttributes.Dispose();
            }
        }
 public static StringFormat StringFormatForAlignment(System.Drawing.ContentAlignment align)
 {
     return(new StringFormat
     {
         Alignment = ControlPaintWrapper.TranslateAlignment(align),
         LineAlignment = ControlPaintWrapper.TranslateLineAlignment(align)
     });
 }
        public static StringFormat CreateStringFormat(Control ctl, ContentAlignment textAlign, bool showEllipsis)
        {
            StringFormat stringFormat = ControlPaintWrapper.StringFormatForAlignment(textAlign);

            if (ctl.RightToLeft == RightToLeft.Yes)
            {
                stringFormat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
            }
            if (showEllipsis)
            {
                stringFormat.Trimming     = StringTrimming.EllipsisCharacter;
                stringFormat.FormatFlags |= StringFormatFlags.LineLimit;
            }
            if (ctl.AutoSize)
            {
                stringFormat.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
            }
            return(stringFormat);
        }