Create a C# WPF app that can open a text file containing information about shape
ID: 3694510 • Letter: C
Question
Create a C# WPF app that can open a text file containing information about shapes to draw and then draw those shapes. Each line of the file represents a single shape to draw. The line starts with the name of the shape (either ellipse or rectangle), followed by three integers for the red, green, and blue for the color, followed by two integers for x and y coordinates for the location of the shape, followed by two more integers for the width and height of the shape. So each line of the file will have this format:
shape red green blue x y width height
Here's an example contents of a file in this format:
ellipse 0 0 255 1 1 100 50
ellipse 255 0 255 150 150 100 100
rectangle 0 255 0 0 300 50 100
To open the file, you will need to use an OpenFileDialog, which might need to be programmatically created in WPF. It's created like a regular control, but you'll have to include the Microsoft.Win32 name space in your file (Add using Microsoft.Win32;).
Explanation / Answer
Solution: please follow these coding as shown in below..
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WPFSample
{
public partial class SetBackgroundColorOfShapeExample : Page
{
public SetBackgroundColorOfShapeExample()
{
// Create a StackPanel to contain the shape.
StackPanel myStackPanel = new StackPanel();
// Create a red Ellipse.
Ellipse myEllipse = new Ellipse();
// Create a SolidColorBrush with a red color to fill the
// Ellipse with.
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
myEllipse.Fill = mySolidColorBrush;
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = Brushes.Black;
// Set the width and height of the Ellipse.
myEllipse.Width = 200;
myEllipse.Height = 100;
// Add the Ellipse to the StackPanel.
myStackPanel.Children.Add(myEllipse);
this.Content = myStackPanel;
}
}
}