<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>JMSoftware &#187; unsafe</title>
	<atom:link href="http://www.jmsoftware.co.uk/blog/tag/unsafe/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jmsoftware.co.uk</link>
	<description>Software Development</description>
	<lastBuildDate>Mon, 26 Sep 2011 13:38:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>C# Snippet for Accessing Bitmap Data with Unsafe Code</title>
		<link>http://www.jmsoftware.co.uk/blog/c-snippet-for-accessing-bitmap-data-with-unsafe-code</link>
		<comments>http://www.jmsoftware.co.uk/blog/c-snippet-for-accessing-bitmap-data-with-unsafe-code#comments</comments>
		<pubDate>Mon, 30 Mar 2009 17:37:55 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[pixel]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[unsafe]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/?p=175</guid>
		<description><![CDATA[Since GetPixel is slow and I keep having to access bitmap data with unsafe code, I thought it would be a nice thing to make into a snippet. Paste the following code into a text file, and save it as &#8230; <a href="http://www.jmsoftware.co.uk/blog/c-snippet-for-accessing-bitmap-data-with-unsafe-code">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since GetPixel is slow and I keep having to access bitmap data with unsafe code, I thought it would be a nice thing to make into a snippet.<span id="more-175"></span></p>
<p>Paste the following code into a text file, and save it as &#8220;usbmp.snippet&#8221;. In Visual Studio go to &#8220;Tools/Code Snippets Manager&#8230;&#8221; and import the file.</p>
<hr />
<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
  &lt;CodeSnippet Format="1.0.0"&gt;
    &lt;Header&gt;
      &lt;Title&gt;Unsafe Bitmap Processing&lt;/Title&gt;
      &lt;Shortcut&gt;usbmp&lt;/Shortcut&gt;
      &lt;Description&gt;Code for accessing a bitmaps pixel data with unsafe code&lt;/Description&gt;
      &lt;Author&gt;Jonathan Mathews Software&lt;/Author&gt;
    &lt;/Header&gt;
    &lt;Snippet&gt;
      &lt;Declarations&gt;
        &lt;Object&gt;
          &lt;ID&gt;bitmap&lt;/ID&gt;
          &lt;Type&gt;System.Drawing.Bitmap&lt;/Type&gt;
          &lt;ToolTip&gt;Replace with a bitmap in your application.&lt;/ToolTip&gt;
          &lt;Default&gt;bitmap&lt;/Default&gt;
        &lt;/Object&gt;
      &lt;/Declarations&gt;
      &lt;Code Language="CSharp"&gt;
        &lt;![CDATA[unsafe
                {
                    BitmapData data = $bitmap$.LockBits(
                                                    new Rectangle(new Point(0, 0), $bitmap$.Size), 
                                                    ImageLockMode.ReadOnly, 
                                                    PixelFormat.Format24bppRgb);

                    byte* pointer = (byte*)data.Scan0;

                    int padding = data.Stride - ($bitmap$.Width * 3);

                    for (int y = 0; y &lt; $bitmap$.Height; y++)
                    {
                        for (int x = 0; x &lt; $bitmap$.Width; x++)
                        {
                            // pointer[2]; // R
                            // pointer[1]; // G
                            // pointer[0]; // B
                            $end$

                            pointer += 3;
                        }

                        pointer += padding;
                    }

                    $bitmap$.UnlockBits(data);
                }]]&gt;
      &lt;/Code&gt;
    &lt;/Snippet&gt;
  &lt;/CodeSnippet&gt;
&lt;/CodeSnippets&gt;</code></pre>
<hr />
<p>Notes:</p>
<ul>
<li>Use it by entering &#8220;usbmp&#8221; in your code, and pressing tab twice. Edit the bitmap name to your bitmap.</li>
<li>You&#8217;ll need to add <code>using System.Drawing.Imaging;</code> if it&#8217;s not there already.</li>
<li>If you want to edit the pixels instead of just reading them, change &#8220;ImageLockMode.ReadOnly&#8221;.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/c-snippet-for-accessing-bitmap-data-with-unsafe-code/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

