2011-09-05 42 views
1

需要一個方法來返回一個變量Texturebrush。該方法需要使用過載:Texturebrush with separator pattern

Size AreaSize, 
int HorizontalSeperatorCount, 
int VerticalSeperatorCount, 
int SeperatorWidth, 
Brush Seperatorbackground, 
Brush Rectanglebackground, 

矩形的尺寸將被自動計算


TextturebrushExample1

這些例子具有以下值

Size AreaSize = new Size(100, 100), 
int HorizontalSeperatorCount = 1, 
int VerticalSeperatorCount = 1, 
int SeperatorWidth = 10, 
Brush Seperatorbackground = Brushes.Grey, 
Brush Rectanglebackground = Brushes.Red 

TextturebrushExample2

第二個例子有不同的VerticalSeperatorCount

Size AreaSize = new Size(100, 100), 
int HorizontalSeperatorCount = 1, 
int VerticalSeperatorCount = 3, 
int SeperatorWidth = 10, 
Brush Seperatorbackground = Brushes.Grey, 
Brush Rectanglebackground = Brushes.Red 

該方法的簽字:

public static TextureBrush GetTextureBrush(Size areaSize, int horizontalSeperator, int verticalSeperatorCount, int seperatorWidth, Brush seperatorBackground, Brush rectangleBackground) 

的的TextureBrush將被用於填充窗口

TextturebrushExample3

我不是最擅長繪圖的東西。 我對解決方案感到非常高興。

回答

0
public static TextureBrush GetSeperatorBrush(Size areaSize, 
    int horizontalSeperatorCount, 
    int verticalSeperatorCount, 
    int seperatorWidth, 
    Brush rectangleBackground) 
{ 
    var horizontalRectangleCount = horizontalSeperatorCount + 1.0f; 
    var verticalRectangleCount = verticalSeperatorCount + 1.0f; 

    var horizontalSeperatorBreadths = (horizontalSeperatorCount + 2.0f) * seperatorWidth; 
    var verticalSeperatorBreadths = (verticalSeperatorCount + 2.0f) * seperatorWidth; 

    var rectangleWidth = (areaSize.Width - verticalSeperatorBreadths)/verticalRectangleCount; 
    var rectangleHeight = (areaSize.Height - horizontalSeperatorBreadths)/horizontalRectangleCount; 

    var bitmap = new Bitmap((int)Math.Ceiling(rectangleWidth + seperatorWidth), (int)Math.Ceiling(rectangleHeight + seperatorWidth)); 
    var graphics = Graphics.FromImage(bitmap); 

    var rectanglePoints = new[] { new PointF(seperatorWidth, seperatorWidth), 
     new PointF(seperatorWidth + rectangleWidth, seperatorWidth), 
     new PointF(seperatorWidth + rectangleWidth, seperatorWidth + rectangleHeight), 
     new PointF(seperatorWidth, seperatorWidth + rectangleHeight), 
     new PointF(seperatorWidth, seperatorWidth)}; 

    graphics.FillPolygon(rectangleBackground, rectanglePoints); 
    graphics.Dispose(); 

    var textureBrush = new TextureBrush(bitmap, System.Drawing.Drawing2D.WrapMode.Tile); 
    return textureBrush; 
} 

由於四捨五入導致多個分離器出現問題。對我來說還好。