Click here to Skip to main content
15,078,238 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
can somebody tell me how to calculate total number of pixels in an image using c#
Posted

The total amount of pixels in a bitmap is the product of PixelHeight and PixelWidth properties. More about this is explained at Bitmaps And Pixel Bits[^].
   
Comments
Sergey Alexandrovich Kryukov 12-Apr-12 21:10pm
   
The only non-nonsense answer to this nonsense question. My 5.
--SA
Try this

Only compatible with .Net FW 4 . Insure that you have that

C#
Bitmap image1;

private void Button1_Click(System.Object sender, System.EventArgs e)
{

    try
    {
        // Retrieve the image.
        image1 = new Bitmap(@"C:\Documents and Settings\All Users\" 
            + @"Documents\My Music\music.bmp", true);

        int x, y;

        // Loop through the images pixels to reset color.
        for(x=0; x<image1.width;>        {
            for(y=0; y<image1.height;>            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image.
        PictureBox1.Image = image1;

        // Display the pixel format in Label1.
        Label1.Text = "Pixel format: "+image1.PixelFormat.ToString();

    }
    catch(ArgumentException)
    {
        MessageBox.Show("There was an error." +
            "Check the path to the image file.");
    }
}
   
Comments
saifullahiit 13-Feb-12 1:45am
   
it just tell me the pixel format. i want to calculate the total pixel no matter what ever the color of pixel is.
RDBurmon 13-Feb-12 1:49am
   
Ahhh . Give me minute. will revert back
RDBurmon 13-Feb-12 1:52am
   
Ok Read this

http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height
RDBurmon 13-Feb-12 1:56am
   
and also read this you will get the way to calculate pixel of image

http://www.ehow.com/how_8232431_calculate-total-pixels.html

Hope this helps if yes then accept and vote the answer otherwise revert back with your queries
--Rahul D.
Dear Friend,

Please follow this link:-

c-sharp-how-to-find-number-of-pixels-from-specific-color-in-a-image[^]

I hope this will help you out.

Thanks
   
Comments
saifullahiit 13-Feb-12 1:46am
   
it just tell me the green,red and blue pixels, i want to calculate the total pixel no matter what ever the color of pixel is.
   
Comments
saifullahiit 13-Feb-12 1:46am
   
it just tell me the green,red and blue pixels, i want to calculate the total pixel no matter what ever the color of pixel is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900