MisterTootor M.S., B.S., A.S., A.S.B
I'm a paragraph. Click here to add your own text and edit me. It's easy.
Draw Graphics On A Bitmap
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
public class DrawGraphicsOnBitmap : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new DrawGraphicsOnBitmap());
}
public DrawGraphicsOnBitmap()
{
Background = Brushes.Khaki;
RenderTargetBitmap renderbitmap = new RenderTargetBitmap(80, 80, 60, 60, PixelFormats.Default);
DrawingVisual drawvis = new DrawingVisual();
DrawingContext dc = drawvis.RenderOpen();
dc.DrawRoundedRectangle(Brushes.Blue, new Pen(Brushes.Red, 10),new Rect(20, 20, 60, 60), 10, 10);
dc.Close();
renderbitmap.Render(drawvis);
Image img = new Image();
img.Source = renderbitmap;
Content = img;
}
}
Draws a circle with a blue interior
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Microsoft.Samples.Graphics.RectangleExample"
WindowTitle="Example">
<Canvas>
<Ellipse
Width="80"
Height="80"
Fill="Red"
Canvas.Left="120"
Canvas.Top="36"/>
</Canvas>
</Page>
Painting a 3D surface with a bitmap
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Viewport3D>
<Viewport3D.Camera>
<PerspectiveCamera Position="0,1,10" LookDirection="0,-1,-10" />
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup>
<DirectionalLight Direction="0,-1,-10" />
<DirectionalLight Direction="3,2,2" />
<GeometryModel3D>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="-1,1,0 1,1,0 -1,-1,0, 1,-1,0"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TextureCoordinates="0,0 1,0 0,1 1,1"
TriangleIndices="0,2,3 0,3,1" />
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial>
<DiffuseMaterial.Brush>
<ImageBrush ImageSource="c\image.jpg" />
</DiffuseMaterial.Brush>
</DiffuseMaterial>
</GeometryModel3D.Material>
</GeometryModel3D>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
</Page>
Play mp3 file
<Window x:Class="SoundAndVideo.MultipleSounds"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MultipleSounds" Height="360" Width="660">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<MediaElement x:Name="media1" Volume="0.25"></MediaElement>
<MediaElement x:Name="media2" Volume="1"></MediaElement>
<Button>
<Button.Content>Click.</Button.Content>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MediaTimeline Source="c:\song.mp3" Storyboard.TargetName="media1"></MediaTimeline>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
<Button Grid.Row="1">
<Button.Content >wav</Button.Content>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MediaTimeline Source="c:\song.wav" Storyboard.TargetName="media2"></MediaTimeline>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
</Window>
Polygon with Fill
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Microsoft.Samples.Graphics.RectangleExample"
WindowTitle="Rectangle">
<Canvas>
<Polygon
Points="9,120 50,10 120,120"
Fill="#FFCCFF" />
</Canvas>
</Page>
Velocity animation
<Page x:Class="Microsoft.Samples.PerFrameAnimations.FrameIndependentFollowExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas x:Name="containerCanvas" Background="Transparent">
<Rectangle x:Name="followRectangle" Canvas.Left="0" Canvas.Top="0"
Fill="blue" Width="60" Height="60" />
</Canvas>
</Page>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace Microsoft.Samples.PerFrameAnimations
{
public partial class FrameIndependentFollowExample : Page
{
private Vector _rectangleVelocity = new Vector(0, 0);
private Point _lastMousePosition = new Point(450, 450);
private TimeSpan _lastRender;
public FrameIndependentFollowExample(): base()
{
_lastRender = TimeSpan.FromTicks(DateTime.Now.Ticks);
CompositionTarget.Rendering += UpdateRectangle;
}
private void UpdateRectangle(object sender, EventArgs e)
{
RenderingEventArgs renderArgs = (RenderingEventArgs)e;
Double deltaTime = (renderArgs.RenderingTime - _lastRender).TotalSeconds;
_lastRender = renderArgs.RenderingTime;
Point location = new Point(0,0);
Vector toMouse = _lastMousePosition - location;
double followForce = 1.00;
_rectangleVelocity += toMouse * followForce;
double drag = 0.9;
_rectangleVelocity *= drag;
location += _rectangleVelocity * deltaTime;
Canvas.SetLeft(followRectangle, location.X);
Canvas.SetTop(followRectangle, location.Y);
}
}
}