[C#.NET] Optical Neopet Reader

Discussion in 'Code Snippets and Tutorials' started by zav75, Apr 7, 2007.

  1. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec
    This is my opr for c# you need to include

    Code (C#):
    1. <div class="csharp" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Drawing</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Text</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">Forms</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li></ol></div>
    In this example I used a little gui made of 1 boutton and 1 picture box...

    Code (C#):
    1. <div class="csharp" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Collections</span>.<span style="color: #0000FF;">Generic</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">ComponentModel</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Data</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Drawing</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Text</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">Forms</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">namespace</span> OPR</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">    <span style="color: #0600FF;">public</span> partial <span style="color: #FF0000;">class</span> Form1 : Form</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">    <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">        <span style="color: #0600FF;">public</span> Form1<span style="color: #000000;">(</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">        <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            InitializeComponent<span style="color: #000000;">(</span><span style="color: #000000;">)</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">        <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> btnImage_Click<span style="color: #000000;">(</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">        <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">           </li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            Bitmap neopetPicture = <span style="color: #000000;">(</span>Bitmap<span style="color: #000000;">)</span> picBox.<span style="color: #0000FF;">Image</span>;          </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #FF0000;">int</span>  now =  DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">Millisecond</span> ;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #FF0000;">int</span> width = neopetPicture.<span style="color: #0000FF;">Width</span>, height = neopetPicture.<span style="color: #0000FF;">Height</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            Color unPixel = Color.<span style="color: #0000FF;">White</span>  ;           <span style="color: #008080; font-style: italic;">// pixel de comparaison </span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            Color darkPixel = Color.<span style="color: #0000FF;">White</span>  ;            </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #FF0000;">int</span> coodX = <span style="color: #FF0000;">0</span>, coodY = <span style="color: #FF0000;">0</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #0600FF;">for</span> <span style="color: #000000;">(</span><span style="color: #FF0000;">int</span> a = <span style="color: #FF0000;">0</span>; a < height; a += <span style="color: #FF0000;">10</span><span style="color: #000000;">)</span> <span style="color: #008080; font-style: italic;">//Here we find teh dark pixel</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                    <span style="color: #0600FF;">for</span> <span style="color: #000000;">(</span><span style="color: #FF0000;">int</span> b = <span style="color: #FF0000;">0</span>; b < width; b += <span style="color: #FF0000;">10</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                    <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                        unPixel = neopetPicture.<span style="color: #0000FF;">GetPixel</span><span style="color: #000000;">(</span>b, a<span style="color: #000000;">)</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">(</span>unPixel.<span style="color: #0000FF;">GetBrightness</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> < darkPixel.<span style="color: #0000FF;">GetBrightness</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                        <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                            darkPixel = unPixel;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                            coodX = b;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                            coodY = a;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                        <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                    <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                <span style="color: #FF0000;">int</span> fin = DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">Millisecond</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                MessageBox.<span style="color: #0000FF;">Show</span><span style="color: #000000;">(</span><span style="color: #808080;">"The neopet is at :"</span>+ coodX.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> + <span style="color: #808080;">", "</span> + coodY.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span>+<span style="color: #808080;">" and was found in "</span> + <span style="color: #000000;">(</span>fin-now<span style="color: #000000;">)</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> +<span style="color: #808080;">" ms."</span><span style="color: #000000;">)</span>;<span style="color: #008080; font-style: italic;">//shows cood</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                <span style="color: #0600FF;">for</span> <span style="color: #000000;">(</span><span style="color: #FF0000;">int</span> p = <span style="color: #FF0000;">0</span>; p < width - <span style="color: #FF0000;">1</span>; p++<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                neopetPicture.<span style="color: #0000FF;">SetPixel</span><span style="color: #000000;">(</span>p, coodY, Color.<span style="color: #0000FF;">White</span><span style="color: #000000;">)</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #0600FF;">for</span> <span style="color: #000000;">(</span><span style="color: #FF0000;">int</span> k = <span style="color: #FF0000;">0</span>; k < height - <span style="color: #FF0000;">1</span>; k++<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">                neopetPicture.<span style="color: #0000FF;">SetPixel</span><span style="color: #000000;">(</span>coodX, k, Color.<span style="color: #0000FF;">White</span><span style="color: #000000;">)</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">            picBox.<span style="color: #0000FF;">Image</span> = neopetPicture;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">        <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">    <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #000000;">}</span></li></ol></div>
    Here some picture of the result

    1- the gui interface...

    [​IMG]

    2- where where he says the neopet is...

    [​IMG]

    3- the time it took

    [​IMG]
     
  2. tac123

    tac123 Level I

    Joined:
    Apr 29, 2007
    Messages:
    124
    Likes Received:
    0
    Nice script, could be improved my threading it and having thread 1 check even rows, thread 2 check odd rows. Of course this is assuming you have a multithreaded processor.