banner



How To Draw A Dynamic Rectangle

  • This lawmaking shows how to describe a rectangle: <!--static rectangle -->
    < Sheet Filigree.Column ="0" Filigree.Row ="0" HorizontalAlignment ="Left" Margin ="50,20,0,0">
    < Rectangle Width ="150" Superlative ="150" Stroke ="Crimson" Fill ="Grayness" StrokeThickness ="2"></ Rectangle >
    </
    Canvas >
    <
    TextBlock Filigree.Column ="0" Grid.Row ="0" HorizontalAlignment ="Center"> Static Rectangle </ TextBlock >
    Depict a rectangle dynamically: <!--dynamic rectangle -->
    < Canvas x : Name ="canvas" Grid.Column ="ane" Grid.Row ="0" Margin ="50,twenty,0,0"></ Sail >
    <
    TextBlock Grid.Column ="one" Grid.Row ="0" HorizontalAlignment ="Centre"> Dynamic Rectangle </ TextBlock > individual void DrawRectangle()
    {

    Rectangle exampleRectangle = new Rectangle();
                exampleRectangle.Width = 150;
                exampleRectangle.Summit = 150;
    // Create a SolidColorBrush and employ it to
    // pigment the rectangle.
    SolidColorBrush myBrush = new SolidColorBrush(Colors.Green);
                exampleRectangle.Stroke = Brushes.Ruby-red;
                exampleRectangle.StrokeThickness = 4;
                exampleRectangle.Make full = myBrush;
                canvas.Children.Insert(0, exampleRectangle);
     }

    Result looks like this:


    Figure 1.

  • This code shows how depict rectangle with Radius.

    <!--static rectangle with radius -->

    < Canvas Grid.Column ="0" Filigree.Row ="ane" HorizontalAlignment ="Left" Margin ="fifty,twenty,0,0">

    < Rectangle Width ="150" Meridian ="150" RadiusX ="10" RadiusY ="10" Stroke ="Crimson" Fill ="Gray" StrokeThickness ="2"></ Rectangle >

    </ Canvas >

    < TextBlock Filigree.Cavalcade ="0" Grid.Row ="1" HorizontalAlignment ="Center"> Static Rectangle Radius </ TextBlock > Brand dynamically: <!--dynamic rectangle with radius -->
    < Sail ten : Name ="canvas1" Filigree.Column ="1" Grid.Row ="ane" Margin ="50,20,0,0"></ Sheet >
    < TextBlock Grid.Cavalcade ="1" Grid.Row ="i" HorizontalAlignment ="Center"> Dynamic Rectangle Radius </ TextBlock >

    individual void RadiusRectangle()

            {

    Rectangle exampleRectangle1 = new Rectangle();

                exampleRectangle1.Width = 150;

                exampleRectangle1.Acme = 150;

                exampleRectangle1.RadiusX = 10;

                exampleRectangle1.RadiusY = x;

    // Create a SolidColorBrush and utilise it to

    // paint the rectangle.

    SolidColorBrush myBrush = new SolidColorBrush(Colors.Green);

                exampleRectangle1.Stroke = Brushes.Red;

                exampleRectangle1.StrokeThickness = iv;

                exampleRectangle1.Fill = myBrush;

                canvas1.Children.Insert(0, exampleRectangle1);

            }

    Consequence:

    Figure two.

  • This code shows how to make a rectangle using Gradient colors.

    <!--static rectangle with gradient colors-->

    < Canvas Grid.Column ="0" Grid.Row ="ii" HorizontalAlignment ="Left" Margin ="50,20,0,0">

    < Rectangle Width ="150" Pinnacle ="150">

    < Rectangle.Fill >

    < LinearGradientBrush >

    < GradientStop Color ="Xanthous" Offset ="0.0" />

    < GradientStop Colour ="Orange" Offset ="0.5" />

    < GradientStop Color ="Red" Offset ="1.0" />

    </ LinearGradientBrush >

    </ Rectangle.Fill >

    </ Rectangle >

    </ Canvass >

    < TextBlock Grid.Column ="0" Grid.Row ="two" HorizontalAlignment ="Center"> Static Rectangle Gradient </ TextBlock > Make Dynamically:

    <!--dynamic rectangle with gradient colors-->

    < Sheet x : Name ="canvas2" Grid.Column ="1" Filigree.Row ="2" Margin ="50,20,0,0"></ Sheet >

    < TextBlock Grid.Column ="ane" Filigree.Row ="2" HorizontalAlignment ="Center"> Dynamic Rectangle Gradient </ TextBlock >

    individual void GradientRectangle()

            {

    Rectangle exampleRectangle = new Rectangle();

                exampleRectangle.Width = 150;

                exampleRectangle.Acme = 150;

    // Create a RadialGradientBrush and use it to

    // paint the rectangle.

    RadialGradientBrush myBrush = new RadialGradientBrush();

                myBrush.GradientOrigin = new Point(0.75, 0.25);

                myBrush.GradientStops.Add(new GradientStop(Colors.Xanthous, 0.0));

                myBrush.GradientStops.Add together(new GradientStop(Colors.Orangish, 0.5));

                myBrush.GradientStops.Add(new GradientStop(Colors.Red, ane.0));

                exampleRectangle.Fill = myBrush;

                canvas2.Children.Insert(0, exampleRectangle);

            }

    Figure 3.

  • This code shows how to draw a rectangle pigment with an image. <!--static paint with image-->

    < Canvas Grid.Column ="0" Grid.Row ="3" HorizontalAlignment ="Left" Margin ="50,xx,0,0">

    < Rectangle Width ="150" Superlative ="150">

    < Rectangle.Fill >

    < ImageBrush ImageSource ="sampleImages\san20a.jpg"  />

    </ Rectangle.Fill up >

    </ Rectangle >

    </ Canvass >

    < TextBlock Grid.Cavalcade ="0" Grid.Row ="3" HorizontalAlignment ="Center"> Static Rectangle paint with paradigm </ TextBlock > Dynamically: <!--dynamic paint with image-->

    < Sheet x : Name ="canvas3" Grid.Cavalcade ="1" Grid.Row ="3" Margin ="50,20,0,0"></ Canvass >

    < TextBlock Grid.Cavalcade ="1" Grid.Row ="3" HorizontalAlignment ="Center"> Dynamic Rectangle pigment with epitome </ TextBlock >

    private void PaintWithImageRectangle()

            {

    Rectangle exampleRectangle = new Rectangle();

                exampleRectangle.Width = 150;

                exampleRectangle.Height = 150;

    // Create an ImageBrush and use it to

    // paint the rectangle.

    ImageBrush myBrush = new ImageBrush();

                myBrush.ImageSource =

    new BitmapImage(new Uri(@"C:\Users\Raj\Documents\Visual Studio 2008\Projects\Chapter1\Chapter1\sampleImages\san20a.jpg", UriKind.Relative));

                exampleRectangle.Fill = myBrush;

                canvas3.Children.Insert(0, exampleRectangle);

            } Result:

    Figure 4.

  • This code shows how to draw paint a rectangle with visual effects.

    <!--static paint with visual-->

    < Canvas Grid.Column ="two" Filigree.Row ="0" HorizontalAlignment ="Left" Margin ="50,20,0,0">

    < Rectangle Width ="150" Height ="150" Stroke ="Red" StrokeThickness ="4">

    < Rectangle.Fill >

    < VisualBrush TileMode ="Tile">

    < VisualBrush.Visual >

    < StackPanel >

    < StackPanel.Background >

    < DrawingBrush >

    < DrawingBrush.Drawing >

    < GeometryDrawing >

    < GeometryDrawing.Brush >

    < RadialGradientBrush >

    < GradientStop Color ="MediumBlue" First ="0.0" />

    < GradientStop Colour ="White" Beginning ="1.0" />

    </ RadialGradientBrush >

    </ GeometryDrawing.Brush >

    < GeometryDrawing.Geometry >

    < GeometryGroup >

    < RectangleGeometry Rect ="0,0,fifty,fifty" />

    < RectangleGeometry Rect ="l,50,fifty,50" />

    </ GeometryGroup >

    </ GeometryDrawing.Geometry >

    </ GeometryDrawing >

    </ DrawingBrush.Drawing >

    </ DrawingBrush >

    </ StackPanel.Groundwork >

    < TextBlock FontSize ="10pt" Margin ="ten"> Raj Beniwal </ TextBlock >

    </ StackPanel >

    </ VisualBrush.Visual >

    </ VisualBrush >

    </ Rectangle.Fill >

    </ Rectangle >

    </ Sail >

    < TextBlock Grid.Column ="2" Grid.Row ="0" HorizontalAlignment ="Center"> Static Rectangle paint with visual </ TextBlock >Dynamically:

    <!--dynamic paint with epitome-->

    <Sheet x:Name="canvas4" Grid.Cavalcade="3" Grid.Row="0" Margin="50,twenty,0,0"></Canvas>

    <TextBlock Grid.Column="3" Filigree.Row="0" HorizontalAlignment="Center">Dynamic Rectangle paint with visual</TextBlock>

    individual void VisualRectangle()

            {

    Rectangle exampleRectangle = new Rectangle();

                exampleRectangle.Width = 150;

                exampleRectangle.Top = 150;

                exampleRectangle.StrokeThickness = 4;

                exampleRectangle.Stroke = Brushes.Red;

    // Create a VisualBrush and use it

    // to pigment the rectangle.

    VisualBrush myBrush = new VisualBrush();

    //

    // Create the brush'due south contents.

    //

    StackPanel aPanel = new StackPanel();

    // Create a DrawingBrush and use it to

    // pigment the console.

    DrawingBrush myDrawingBrushBrush = new DrawingBrush();

    GeometryGroup aGeometryGroup = new GeometryGroup();

                aGeometryGroup.Children.Add together(new RectangleGeometry(new Rect(0, 0, fifty, 50)));

                aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(l, fifty, 50, 50)));

    RadialGradientBrush checkerBrush = new RadialGradientBrush();

                checkerBrush.GradientStops.Add(new GradientStop(Colors.Green, 0.0));

                checkerBrush.GradientStops.Add(new GradientStop(Colors.White, 1.0));

    GeometryDrawing checkers = new GeometryDrawing(checkerBrush, aught, aGeometryGroup);

                myDrawingBrushBrush.Drawing = checkers;

                aPanel.Background = myDrawingBrushBrush;

    // Create some text.

    TextBlock someText = new TextBlock();

                someText.Text = "Raj Beniwal";

    FontSizeConverter fSizeConverter = new FontSizeConverter();

                someText.FontSize = (double)fSizeConverter.ConvertFromString("10pt");

                someText.Margin = new Thickness(10);

                aPanel.Children.Add(someText);

                myBrush.Visual = aPanel;

                exampleRectangle.Fill = myBrush;

                canvas4.Children.Insert(0, exampleRectangle);

            }

    Figure v.

  • This code shows how to draw and pigment a rectangle with drawing brush.

    <!--static paint with drawing-->

    < Canvas Filigree.Column ="2" Filigree.Row ="1" HorizontalAlignment ="Left" Margin ="fifty,20,0,0">

    < Rectangle Width ="150" Peak ="150">

    < Rectangle.Fill >

    < DrawingBrush Viewport ="0,0,0.25,0.25" TileMode ="Tile">

    < DrawingBrush.Drawing >

    < DrawingGroup >

    < GeometryDrawing Brush ="White">

    < GeometryDrawing.Geometry >

    < RectangleGeometry Rect ="0,0,100,100" />

    </ GeometryDrawing.Geometry >

    </ GeometryDrawing >

    < GeometryDrawing >

    < GeometryDrawing.Geometry >

    < GeometryGroup >

    < RectangleGeometry Rect ="0,0,fifty,50" />

    < RectangleGeometry Rect ="50,50,50,50" />

    </ GeometryGroup >

    </ GeometryDrawing.Geometry >

    < GeometryDrawing.Brush >

    < LinearGradientBrush >

    < GradientStop Offset ="0.0" Color ="Red" />

    < GradientStop Offset ="1.0" Color ="Light-green" />

    </ LinearGradientBrush >

    </ GeometryDrawing.Brush >

    </ GeometryDrawing >

    </ DrawingGroup >

    </ DrawingBrush.Drawing >

    </ DrawingBrush >

    </ Rectangle.Fill up >

    </ Rectangle >

    </ Sail >

    < TextBlock Grid.Cavalcade ="ii" Grid.Row ="one" HorizontalAlignment ="Center"> Static Rectangle paint with cartoon </ TextBlock > Dynamic:

    <!--dynamic pigment with drawing-->

    < Canvas ten : Proper name ="canvas5" Filigree.Cavalcade ="3" Grid.Row ="i" Margin ="fifty,twenty,0,0"></ Canvas >

    < TextBlock Grid.Column ="iii" Grid.Row ="1" HorizontalAlignment ="Center"> Dynamic Rectangle paint with cartoon </ TextBlock >

    private void PaintWithDrawing()

            {

    Rectangle exampleRectangle = new Rectangle();

                exampleRectangle.Width = 150;

                exampleRectangle.Height = 150;

    // Create a DrawingBrush and utilize information technology to

    // paint the rectangle.

    DrawingBrush myBrush = new DrawingBrush();

    GeometryDrawing backgroundSquare =

    new GeometryDrawing(

    Brushes.White,

    aught,

    new RectangleGeometry(new Rect(0, 0, 100, 100)));

    GeometryGroup aGeometryGroup = new GeometryGroup();

                aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, fifty, 50)));

                aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(50, l, 50, 50)));

    LinearGradientBrush checkerBrush = new LinearGradientBrush();

                checkerBrush.GradientStops.Add(new GradientStop(Colors.Scarlet, 0.0));

                checkerBrush.GradientStops.Add(new GradientStop(Colors.Greenish, 1.0));

    GeometryDrawing checkers = new GeometryDrawing(checkerBrush, nix, aGeometryGroup);

    DrawingGroup checkersDrawingGroup = new DrawingGroup();

                checkersDrawingGroup.Children.Add(backgroundSquare);

                checkersDrawingGroup.Children.Add(checkers);

                myBrush.Drawing = checkersDrawingGroup;

                myBrush.Viewport = new Rect(0, 0, 0.25, 0.25);

                myBrush.TileMode = TileMode.Tile;

                exampleRectangle.Fill = myBrush;

                canvas5.Children.Insert(0, exampleRectangle);

            }

    Result:

    Effigy 6.

  • This code shows how to draw and fill a rectangle with a brush and opacity (transparency). The Opacity property defines the transparency of a control in XAML and WPF. <!--static rectangle with brush -->

    < Canvass Grid.Column ="2" Filigree.Row ="2" HorizontalAlignment ="Left" Margin ="50,20,0,0">

    < Rectangle Width ="150" Elevation ="150">

    < Rectangle.Fill >

    < SolidColorBrush Color ="Green" Opacity ="0.25" />

    </ Rectangle.Fill >

    </ Rectangle >

    </ Sheet >

    < TextBlock Filigree.Column ="2" Grid.Row ="2" HorizontalAlignment ="Center"> Static Rectangle rectangle with castor </ TextBlock >

    Dynamic:

    <!--dynamic rectangle using castor-->

    < Canvass x : Name ="canvas6" Filigree.Column ="3" Filigree.Row ="2" Margin ="50,20,0,0"></ Canvas >

    < TextBlock Grid.Column ="3" Grid.Row ="2" HorizontalAlignment ="Heart"> Dynamic Rectangle paint with drawing </ TextBlock >

    private void RectangleWithBrush()

            {

    Rectangle myRectangle = new Rectangle();

                myRectangle.Width = 150;

                myRectangle.Peak = 150;

    SolidColorBrush partiallyTransparentSolidColorBrush

                    = new SolidColorBrush(Colors.Dark-green);

                partiallyTransparentSolidColorBrush.Opacity = 0.25;

                myRectangle.Fill = partiallyTransparentSolidColorBrush;

                canvas6.Children.Insert(0, myRectangle);

            }

    Result:

    Effigy 7.

  • This demonstrate how to rotate a rectangle using transformation. The RenderTransform property of Rectangle is responsible for transforming a rectangle such as rotating.

    <!--static rotate rectangle -->

    < Canvas Filigree.Column ="two" Filigree.Row ="three" HorizontalAlignment ="Left" Margin ="50,xx,0,0">

    < Rectangle Width ="150" Elevation ="150" Stroke ="#FFBF4343" Canvas.Left ="10" Sail.Superlative ="10" StrokeThickness ="4" RenderTransformOrigin ="0.5,0.v">

    < Rectangle.RenderTransform >

    < TransformGroup >

    < ScaleTransform ScaleX ="1" ScaleY ="1"/>

    < SkewTransform AngleX ="0" AngleY ="0"/>

    < RotateTransform Bending ="30.704"/>

    < TranslateTransform X ="0" Y ="0"/>

    </ TransformGroup >

    </ Rectangle.RenderTransform >

    < Rectangle.Fill >

    < LinearGradientBrush EndPoint ="0.5,1" StartPoint ="0.5,0">

    < GradientStop Color ="#FF000000" Get-go ="0"/>

    < GradientStop Colour ="#FF1E1919" Outset ="1"/>

    </ LinearGradientBrush >

    </ Rectangle.Fill up >

    </ Rectangle >

    </ Canvas >

    < TextBlock Grid.Cavalcade ="2" Grid.Row ="3" HorizontalAlignment ="Centre"> Static Rotate Rectangle </ TextBlock >

    Consequence:


    Figure 8.

  • For more data encounter attached project. This is it.

    Source: https://www.c-sharpcorner.com/uploadfile/raj1979/dynamic-and-static-rectangle-in-wpf/

    Posted by: blancharddeve1941.blogspot.com

    0 Response to "How To Draw A Dynamic Rectangle"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel