WPF / Silverlight实时渲染模糊效果 [英] WPF/Silverlight rendering blur effect in realtime

查看:101
本文介绍了WPF / Silverlight实时渲染模糊效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF项目中,我完成了点击一些UI元素这样的东西:



In my WPF project I''ve done with clicking some UI element such stuff:

void vb1_click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    DispatcherTimer dt = new DispatcherTimer();
    dt.Interval = new TimeSpan(0, 0, 0, 0, 1000);
    dt.Tick += new System.EventHandler(dt_Tick);
    dt.Start();
}

void dt_Tick(object sender, System.EventArgs e)
{
    for(int i = 0; i < 20; i++)
    {
        this.vb2_blur_eff.Radius = (double)i;
    }
}





主要问题是:我看不到每秒模糊效果的每一步。程序是空闲的,当过程完成时,它给了我最后一步模糊UI元素。



我不喜欢不想要它,因为两个程序空转都看起来很难看,我想在每个时期看到渲染的结果。



The main problem is: "I can''t see the each step of blur-effect per second. The program is idling and when the process is complete it gives me the final step of bluring the UI element.

I don''t want it, cause both program idling is looking ugly and I want actually see the rendered result at the each period.

推荐答案

你好我认为你选择了一个坏的使用Dispatcher计时器的方法,首先阅读MSDN所说的内容:

Hi i think you have choosen a bad approach with usage of Dispatcher timer, first of all read what MSDN said about it:
<br />
The DispatcherTimer is reevaluated at the top of every DispatcherTimer loop.<br />
Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the DispatcherTimer queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.





改进::





Improvements::

public static class ApplicationHelper
	{
		[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
		public static void DoEvents(DispatcherPriority priority)
		{
			DispatcherFrame frame = new DispatcherFrame();
			DispatcherOperation oper = Dispatcher.CurrentDispatcher.BeginInvoke(priority,
				new DispatcherOperationCallback(ExitFrameOperation), frame);

			Dispatcher.PushFrame(frame);
			if (oper.Status != DispatcherOperationStatus.Completed)
			{
				oper.Abort();
			}
		}

		private static object ExitFrameOperation(object obj)
		{
			((DispatcherFrame)obj).Continue = false;
			return null;
		}

		[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
		public static void DoEvents()
		{
			DoEvents(DispatcherPriority.Background);
		}
	}





所以,让我们总结一下:





So , let''s summarize :

void vb1_click(object sender, System.Windows.Input.MouseButtonEventArgs e)
   {
       DispatcherTimer dt = new DispatcherTimer();
       dt.Interval = new TimeSpan(0, 0, 0, 0, 1000);
       dt.Tick += new System.EventHandler(dt_Tick);
       dt.Start();
   }

   void dt_Tick(object sender, System.EventArgs e)
   {
       for(int i = 0; i < 20; i++)
       {
           this.vb2_blur_eff.Radius = (double)i;
           ApplicationHelper.DoEvents();

       }
   }


这篇关于WPF / Silverlight实时渲染模糊效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆