<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog Informatico &#187; Flickering</title>
	<atom:link href="http://www.bloginformatico.net/tag/flickering/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bloginformatico.net</link>
	<description>L'informatica come non l'avete mai letta</description>
	<lastBuildDate>Fri, 30 Apr 2010 16:23:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Diminuire l&#8217;effetto flickering aumentando le prestazioni in GDI+</title>
		<link>http://www.bloginformatico.net/2009/03/12/diminuire-leffetto-flickering-aumentando-le-prestazioni-in-gdi/</link>
		<comments>http://www.bloginformatico.net/2009/03/12/diminuire-leffetto-flickering-aumentando-le-prestazioni-in-gdi/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 16:32:10 +0000</pubDate>
		<dc:creator>Massimo</dc:creator>
				<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Flickering]]></category>
		<category><![CDATA[GDI+]]></category>
		<category><![CDATA[Windows Form]]></category>

		<guid isPermaLink="false">http://www.bloginformatico.net/?p=140</guid>
		<description><![CDATA[Quando si iniziare ad utilizzare GDI+ nel .NET Framework il primo problema da affrontare è il così detto effetto flickering. Il flickering è quel fastidioso sfarfallio che vediamo quando disegniamo un oggetto in GDI+, come un rettangolo o un cerchio. Esistono numerosi trucchetti per togliere, almeno parzialmente, questo problema e contemporaneamente aumentare le prestazioni grafiche. [...]]]></description>
			<content:encoded><![CDATA[<p>Quando si iniziare ad utilizzare GDI+ nel .NET Framework il primo problema da affrontare è il così detto effetto <em><a href="http://en.wikipedia.org/wiki/Flicker_(screen)">flickering</a></em>. Il flickering è quel fastidioso sfarfallio che vediamo quando disegniamo un oggetto in <strong>GDI+</strong>, come un rettangolo o un cerchio.</p>
<p>Esistono numerosi trucchetti per togliere, almeno parzialmente, questo problema e contemporaneamente aumentare le prestazioni grafiche. Il primo consiglio è quello di impostare la proprietà <em><strong>DoubleBuffer</strong></em> della Windows Form a <em><strong>True</strong></em>. Anche se il consumo di memoria aumenterà le prestazioni ne beneficeranno. Inoltre è bene impostare altre proprietà interessanti. Possiamo fare tutto in un colpo solo impostando un&#8217;insieme di stili nel costruttore della windows form. In questo modo:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> Form1()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">this</span>.InitializeComponent();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">this</span>.SetStyle(ControlStyles.UserPaint, <span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">this</span>.SetStyle(ControlStyles.AllPaintingInWmPaint, <span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">this</span>.SetStyle(ControlStyles.DoubleBuffer, <span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">this</span>.SetStyle(ControlStyles.ResizeRedraw, <span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">this</span>.SetStyle(ControlStyles.OptimizedDoubleBuffer, <span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>     <span style="color: #0000ff">this</span>.UpdateStyles();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  11:</span> }</pre>
</div>
</div>
<p>Questo sistema dovrebbe bastare per ridurre notevolmente il flickering. Ma se volete spingere al massimo le prestazioni c&#8217;è ancora una cosa che si può fare: utilizzare un&#8217;immagine bitmap come buffer ulteriore. In questo modo gli oggetti vengono prima disegnati sulla bitmap di buffer e successivamente la bitmap viene a sua volta renderizzata sulla form. In questo modo:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> Form1_Paint(<span style="color: #0000ff">object</span> sender, PaintEventArgs e)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   3:</span>     <span style="color: #008000">// Creo una bitmap in memoria.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     Bitmap buffer = <span style="color: #0000ff">new</span> Bitmap(<span style="color: #0000ff">this</span>.Width, <span style="color: #0000ff">this</span>.Height);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   5:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>     <span style="color: #008000">// Utilizzo l'oggetto Graphics della bitmap</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   7:</span>     <span style="color: #008000">// e disegno tutti i miei oggetti.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">using</span> (Graphics graphics = Graphics.FromImage(buffer))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   9:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>         <span style="color: #008000">// disegno tutti i miei oggetti.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  11:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  13:</span>     <span style="color: #008000">// Pulisco la superficie della form e disegno</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>     <span style="color: #008000">// l'immagine che ho in memoria.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  15:</span>     e.Graphics.Clear(<span style="color: #0000ff">this</span>.BackColor);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>     e.Graphics.DrawImage(buffer, 0, 0, buffer.Width, buffer.Height);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  17:</span> }</pre>
</div>
</div>
<p>Le prestazioni sono garantite. <img src='http://www.bloginformatico.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bloginformatico.net/2009/03/12/diminuire-leffetto-flickering-aumentando-le-prestazioni-in-gdi/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
