October 2009
M T W T F S S
    Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Categories

  • [+]ASP.NET (7) 
  • [—]C# (87) 
  • [+]C++ (13) 
  • [+]Delphi (20) 
  • [+]JavaScript (24) 
  • [+]Regular Expressions (7) 
  • [+]SQL (13) 
  • [+]VB (100) 
  • [+]VB.NET (4) 

Online

Users: 13 Guests
  • Loading...


    Loading...

    Login






    Register | Lost password?

    Register





    A password will be mailed to you.
    Log in | Lost password?

    Retrieve password





    A confirmation mail will be sent to your e-mail address.
    Log in | Register
  • Fast Greyscale using unsafe code in C#

    /*
      Fast Greyscale using unsafe code in C#
      --
      Insert this as a function anywere, I used it in a seperate DLL
      to keep my form code clean.  Just pass the Bitmap and the picture
      box to show it in to the function
    
      If anyone has tried using the .NET Graphics API, they know that
      replacing pixel colors takes a long time to complete. I did some
      research and found a good source. This code will adjust the color
      to greyscale by Binary.
      The page is http://www.navicosoft.com/software_articles/softwares_articles_index.html
      for more information. It is under Basic Image Processing in the
      list of articles.
    */
    
    public void greyscale(Bitmap bmp, PictureBox picBox)
    {
           BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
               ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
           unsafe
           {
               byte* imgPtr = (byte*)(data.Scan0);
               byte red, green, blue;
               for (int i = 0; i < data.Height; i++)
               {
                   for (int j = 0; j < data.Width; j++)
                   {
                       blue = imgPtr[0];
                       green = imgPtr[1];
                       red = imgPtr[2];
    
                       imgPtr[0] = imgPtr[1] = imgPtr[2] =
                          (byte)(.299 * red
                           + .587 * green
                           + .114 * blue);
                       imgPtr += 3;
                   }
                   imgPtr += data.Stride - data.Width * 3;
               }
    
           }
           bmp.UnlockBits(data);
           picBox.Image = bmp;
    }
    
    Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Digg
    • del.icio.us
    • Bloglines
    • Facebook
    • Google Bookmarks
    • LinkedIn
    • Technorati
    • TwitThis
    • Webnews

    Leave a Reply

     

     

     

    You can use these HTML tags

    <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">

    Spam Protection by WP-SpamFree Plugin