C# WPF中利用UDP制作实时绘图程序老是出现进程被占用,求高手解决
下面是XAML:
<Window x:Class="UDP绘图.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Background="Black" >
<Canvas>
<Canvas Name="win" Width="218" Height="201" Canvas.Left="1" Canvas.Top="0" MouseMove="win_MouseMove"/>
<Canvas Name="win1" Width="200" Height="200" Canvas.Left="303" Canvas.Top="1" />
</Canvas>
</Window>
CS文件代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Windows.Threading;
using System.IO;
namespace UDP绘图
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private UdpClient ucc;
private IPEndPoint iep;
private Thread th;
RenderTargetBitmap bmp;//将visual对象转换为位图
FormatConvertedBitmap fbmp;//为Bitmapsouce提供像素格式转换功能
WriteableBitmap wb;//提供一个可写入并可更新的Bitmapsouce
Int32Rect rect;//描述整数矩形的宽度,高度,位置
byte[] pixelsrc, pixelnew;//表示一个8位无符号整数
int stride;
RenderTargetBitmap bmp1;//将visual对象转换为位图
FormatConvertedBitmap fbmp1;//为Bitmapsouce提供像素格式转换功能
WriteableBitmap wb1;//提供一个可写入并可更新的Bitmapsouce
Int32Rect rect1;//描述整数矩形的宽度,高度,位置
byte[] pixelsrc1, pixelnew1;//表示一个8位无符号整数
int stride1;
// MainWindow main = new MainWindow();
Point pt;
Point pt2;
int bitmapwidth = 525;
int bitmapheight = 350;
public delegate void weituo(Point pt2);//定义委托
DispatcherTimer dispatcherTimer = new DispatcherTimer()
{
Interval = TimeSpan.FromMilliseconds(1)
};
//委托定义
public MainWindow()
{
InitializeComponent();
// weituo weituo1 = chonghui;
bmp = new RenderTargetBitmap(218, 201, 96, 96, PixelFormats.Pbgra32);
fbmp = new FormatConvertedBitmap(bmp, PixelFormats.Bgr24, null, 0);
rect = new Int32Rect(0, 0, 218, 201);
stride = bitmapwidth * 3;
pixelsrc = new byte[stride * 218];
pixelnew = new byte[stride * 201];
wb = new WriteableBitmap(218, 201, 96, 96, PixelFormats.Bgr24, null);
wb.WritePixels(rect, pixelsrc, stride, 0);
win.Background = new ImageBrush(wb);
bmp1 = new RenderTargetBitmap(218, 201, 96, 96, PixelFormats.Pbgra32);
fbmp1 = new FormatConvertedBitmap(bmp1, PixelFormats.Bgr24, null, 0);
rect1 = new Int32Rect(0, 0, 218, 201);
stride1 = 218 * 3;
pixelsrc1 = new byte[stride1 * 218];
pixelnew1 = new byte[stride1 * 201];
wb1 = new WriteableBitmap(218, 201, 96, 96, PixelFormats.Bgr24, null);
wb1.WritePixels(rect1, pixelsrc1, stride1, 0);
win1.Background = new ImageBrush(wb1);
dispatcherTimer.Tick += new EventHandler(Rendering);
dispatcherTimer.Start();
ucc = new UdpClient(8888);
}
private void Rendering(object sender, EventArgs e)
{
pt2 = Mouse.GetPosition(win);
iep=new IPEndPoint(IPAddress.Parse("192.168.1.183"),8888);
th = new Thread(new ThreadStart(listen));
th.IsBackground = true;
th.Start();
Byte[] sendBytes = Encoding.ASCII.GetBytes(Convert.ToString(pt2));
ucc.Send(sendBytes, sendBytes.Length,iep);
}
public void listen()
{
Byte[] receiveBytes = ucc.Receive(ref iep);
string returnData = Encoding.ASCII.GetString(receiveBytes);
//Console.WriteLine("This is the message you received " + returnData);
// Console.WriteLine("This message was sent from " + iep.Address.ToString() + " on their port number " + iep.Port.ToString());
string x = ",";
string[] s = returnData.Split(x.ToCharArray(), 2);
string a = s[0];
string b = s[1];
Point pt2 = new Point();
pt2.X = Convert.ToDouble(a);
pt2.Y = Convert.ToDouble(b);
bubblesort(pt2,weituo1);
// ucc.Close();
}
public static void bubblesort(Point pt2, weituo weituo1)
{
weituo1(pt2);
}
public void weituo1(Point pt2) {
DrawingVisual drawingVisual1 = new DrawingVisual();
DrawingContext drawingContext1 = drawingVisual1.RenderOpen();
drawingContext1.DrawEllipse(Brushes.White, null, new Point(pt2.X, pt2.Y), 2, 2);
drawingContext1.Close();
bmp1.Render(drawingVisual1);//********错误位置
fbmp1.Source = bmp1;
fbmp1.CopyPixels(pixelsrc1, stride1, 0);
wb1.WritePixels(rect1, pixelsrc1, stride1, 0);
}
public void win_MouseMove(object sender, MouseEventArgs e)
{
pt = Mouse.GetPosition(win);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawEllipse(Brushes.White, null, new Point(pt.X, pt.Y), 2, 2);
drawingContext.Close();
bmp.Render(drawingVisual);
fbmp.Source = bmp;
fbmp.CopyPixels(pixelsrc, stride, 0);
wb.WritePixels(rect, pixelsrc, stride, 0);
//DrawingVisual drawingVisual1 = new DrawingVisual();
//DrawingContext drawingContext1 = drawingVisual1.RenderOpen();
//drawingContext1.DrawEllipse(Brushes.White, null, new Point(pt.X, pt.Y), 2, 2);
//drawingContext1.Close();
//bmp1.Render(drawingVisual1);
//fbmp1.Source = bmp1;
//fbmp1.CopyPixels(pixelsrc1, stride1, 0);
//wb1.WritePixels(rect1, pixelsrc1, stride1, 0);
}
}
}
求高手解决啊~在线等