May 24, 2009

Get RGB Color at a Point in an Image with C#.Net

It is very simple to get RGB Color at specific point in an image. For this purpose we need to include System.Drawing Namespace in our code.
using System.Drawing;
Steps as below:
  • Define a point at which, you want to find the color.
  • Get the image loaded.
  • Use method GetPixel from Image class instance to get color at a point.
 Bitmap img = Image.FromFile(@"<Image_file_path>"as Bitmap;
 Color colorAtPoint = img.GetPixel(10, 10);
This is simple method getting color at a point.

3 comments:

  1. Bitmap img = Image.FromFile("<>");
    it can't ....
    Error 1 Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?) C:\Users\Filbert\Documents\Visual Studio 2005\Projects\WindowsApplication7\WindowsApplication7\Form1.cs 25 28 WindowsApplication7

    ReplyDelete
  2. Bitmap img = Image.FromFile("<>");
    it can't ....
    Error 1 Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?) C:\Users\Filbert\Documents\Visual Studio 2005\Projects\WindowsApplication7\WindowsApplication7\Form1.cs 25 28 WindowsApplication7

    ReplyDelete
    Replies
    1. Whenever you see this kind of error of implicit conversion, try to convert it explicitly. I have updated the code snippet for this error. I wonder how come I did not encountered it.

      Delete