<?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>Tony Williams &#187; Image Processing</title>
	<atom:link href="http://blog.tonywilliams.me.uk/tag/image-processing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tonywilliams.me.uk</link>
	<description></description>
	<lastBuildDate>Tue, 10 Jan 2012 12:31:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Find the transparent pixels in an image &#8211; WPF</title>
		<link>http://blog.tonywilliams.me.uk/find-the-transparent-pixels-in-an-image-wpf/</link>
		<comments>http://blog.tonywilliams.me.uk/find-the-transparent-pixels-in-an-image-wpf/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 14:12:31 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Image Processing]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF 4]]></category>

		<guid isPermaLink="false">http://blog.tonywilliams.me.uk/?p=51</guid>
		<description><![CDATA[Same as the title]]></description>
			<content:encoded><![CDATA[<p>Extracted from: <a href="http://random.tonywilliams.me.uk/post/392778744/find-the-transparent-pixels-in-an-image-wpf">http://random.tonywilliams.me.uk/post/392778744/find-the-transparent-pixels-in-an-image-wpf</a></p>
<div>
<div>
<p>The other day I had to create an image element that contained a png with transparent sections and have it display a hand cursor when the mouse was over the opaque sections.</p>
<p>The first thing I tried (and failed) was to have an image element with the cursor property set to &#8220;Hand&#8221; like so:</p>
<pre class="brush:html">
&lt;Image Source="{Binding SomeImage}" Cursor="Hand" /&gt;
</pre>
<p>That obviously never worked so I was forced to look at each individual pixel and see if it was transparent. What I ended up with was a function that generated a bunch of coordinates.</p>
<p>This is that function:</p>
<pre class="brush:c#">private static Dictionary&lt;Point, object&gt; FindTransparentPixelCoordinates(BitmapSource source)
{
  var points = new Dictionary&lt;Point, object&gt;();

  // Convert the source if the pixel format is not what we expect
  if (source.Format != PixelFormats.Bgra32)
  {
    source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
  }

  int width = source.PixelWidth;
  int height = source.PixelHeight;
  var bytes = new byte[width * height * 4];

  source.CopyPixels(bytes, width * 4, 0);

  var position = 0;

  for (var y = 0; y &lt; height; y++)
  {
    for (var x = 0; x &lt; width; x++)
    {
      var byteOffset = position;

      // The pixel format is stored as 32-bits (4 bytes) with the last byte being the alpha

      if (bytes[byteOffset + 3] == 0)
      {
        points.Add(new Point(x, y), null);
      }

      position += 4;
    }
  }

  return points;
}</pre>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.tonywilliams.me.uk/find-the-transparent-pixels-in-an-image-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

