8,735
社区成员
发帖
与我相关
我的任务
分享
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" >
<Button x:Name="button_draw_line" Content="画线" Height="20" Width="100" VerticalAlignment="Top" Click="button_draw_line_Click" ></Button>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
private Random r=new Random();
private SolidColorBrush s = new SolidColorBrush(Colors.Blue);
private void draw_random_line(int line_count)
{
Line l;
for (int i = 0; i < line_count; i++)
{
l = new Line();
l.X1 = r.NextDouble() * LayoutRoot.ActualWidth ;
l.Y1 = r.NextDouble() * LayoutRoot.ActualHeight;
l.X2 = r.NextDouble() * LayoutRoot.ActualWidth;
l.Y2 = r.NextDouble() * LayoutRoot.ActualHeight;
l.Stroke = s;
l.StrokeThickness = 1;
LayoutRoot.Children.Add(l);
}
}
public MainPage()
{
InitializeComponent();
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
}
private void button_draw_line_Click(object sender, RoutedEventArgs e)
{
draw_random_line(1000);
}
}
}
Imports System.Diagnostics
Public Class Lines : Inherits FrameworkElement
Dim gVisual As New DrawingVisual
Public Sub Refresh()
'MyBase.OnRender(drawingContext)
Dim r As New Random
Const LineCount As Integer = 20000
Dim MidlLine As Integer = ActualHeight / 2
Dim p As New Pen(Brushes.Green, 1)
Dim watcher As New Stopwatch
Dim p1 As New Point, p2 As New Point
watcher.Start()
Dim g As Windows.Media.DrawingContext = gVisual.RenderOpen
For i As Single = 0 To LineCount
Dim Lenght As Integer = r.Next(0, ActualHeight)
p1.X = i : p1.Y = MidlLine - Lenght / 2
p2.X = i : p2.Y = MidlLine + Lenght / 2
g.DrawLine(p, p1, p2)
Next
g.Close()
Me.InvalidateVisual()
watcher.Stop()
MsgBox(watcher.ElapsedMilliseconds)
End Sub
Protected Overrides Function GetVisualChild(index As Integer) As System.Windows.Media.Visual
If index = 0 Then Return gVisual
Throw New ArgumentOutOfRangeException
End Function
Protected Overrides ReadOnly Property VisualChildrenCount As Integer
Get
Return 1
End Get
End Property
Public Sub New()
AddVisualChild(gVisual)
AddLogicalChild(gVisual)
End Sub
Imports System.Diagnostics
Public Class Lines_Geometry : Inherits FrameworkElement
Protected Overrides Sub OnRender(drawingContext As System.Windows.Media.DrawingContext)
'# 一些测试用数据
Dim r As New Random
Const LineCount As Integer = 20000
Dim watcher As New Stopwatch
'# 绘图相关对象
Dim MidlLine As Integer = ActualHeight / 2
Dim p As New Pen(Brushes.Green, 1)
Dim p1 As New Point, p2 As New Point
Dim gStream As New StreamGeometry
'# 绘制到StreamGeometry
watcher.Start()
Dim g As Windows.Media.StreamGeometryContext = gStream.Open
For i As Integer = 0 To LineCount
Dim Lenght As Integer = r.Next(0, ActualHeight)
p1.X = i : p1.Y = MidlLine - Lenght / 2
p2.X = i : p2.Y = MidlLine + Lenght / 2
g.BeginFigure(p1, True, False)
g.LineTo(p2, True, False)
Next
g.Close()
'# 绘制到FrameWorkElement
drawingContext.DrawDrawing(New GeometryDrawing(Brushes.Green, New Pen(Brushes.Green, 1), gStream))
'# 显示消耗时间
watcher.Stop()
MainForm.Title = watcher.ElapsedMilliseconds
End Sub
End Class