1 public partial class ucImageCreateView : UserControl 2 { 3 private Rect _PropertyName; 4 private ImageService imageService; 5 public ucImageCreateView() 6 { 7 InitializeComponent(); 8 imageService =new ImageService(200,200); 9 image1.Source=imageService.BitmapSource; 10 imageService.AddByted+= new ImageService.AddByteEventHandle(imageService_AddByted); 11 m_timer.Start(); 12 m_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 13 } 14 15 16 public delegate void OnImageSourceChange(); 17 public void ImageSourceChange() 18 { 19 image1.Source = imageService.BitmapSource; 20 } 21 public void imageService_AddByted(object sender,AddByteEventArgs arg) 22 { 23 image1.Source = imageService.BitmapSource; 24 } 25 26 27 public byte[] GetFrameByte() 28 { 29 Random ra = new Random(); 30 byte[] frameByte =new byte[200]; 31 ra.NextBytes(frameByte); 32 return frameByte; 33 } 34 35 System.Timers.Timer m_timer = new System.Timers.Timer(1500); 36 37 private void OnTimedEvent(object source, ElapsedEventArgs e) 38 { 39 //button1_Click(null, null); 40 41 } 42 43 private int i = 0; 44 private void button1_Click(object sender, RoutedEventArgs e) 45 { 46 if (i < 200) 47 { 48 imageService.AddImageBytes(i, GetFrameByte()); 49 i++; 50 } 51 } 52 }
问题是这样的,button中调用的函数,是我向ImageService中添加byte数组,然后通过ImageServire处理成BitMapSource赋值给image1.Source然后绘制图片。
现在我直接用button点击,触发点击事件,可以做绘制,但是如果用timer去触发button的点击事件,就不能完成绘制,希望高手帮忙解决下。
我之前没做button直接在timer中加入添加byte数组也不能出图像。
以下是利用System.Threading.Time定时执行某个方法的示例代码:
1.using System.Threading;
Timer timer; //System.Threading;
//定时执行下面定义的GetScore方法,3秒执行一次。
timer = new Timer(GetScore, this, 1000, 3000);
//定义一个方法
private void GetScore(object obj)
{
var cclient = ((ucImageCreateView)obj);
cclient.Dispatcher.BeginInvoke(delegate()
{
try
{
//执行你想要的操作
}
catch
{ }
});
}