Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.
I have opened an image in the background panel. I would like to get colour of pixel and that Hue-Saturation-Brightness values. I have a code which accomplishes thsi in another part of my program but I am not sure what to call my image when it is opened in background panel. I get error messages saying the name 'myImage' does not exist in the current context.
thanks

C#
private void button1_Click(object sender, EventArgs e)
{
    openFileDialog1.Filter = "JPEG IMAGES|*.jpg";
    openFileDialog1.InitialDirectory = "C:\\Users\\jason\\Documents\\IProject\\code\\imageAlign\\imageAlign\\bin\\Debug";
    openFileDialog1.Title = "Open Image";
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
   {
        panel1.BackgroundImage = Image.FromFile(openFileDialog1.FileName);
        panel1.Invalidate();
   }
    // Array for keeping the sums of each row of pixels
    float[] resultArray = new float[myImage.Height];

    // Populate the array with data from each row of pixels, using the brightness value
    for (int i = 0; i < myImage.Height; i++)
    {
        float value = 0;
        for (int j = 0; j < myImage.Width; j++)
        {
            value += myImage.GetPixel(j, i).GetBrightness();//get colour of pixel and that Hue-Saturation-Brightness values
        }
        resultArray[i] = value;
    }
}
Posted
Comments
Thomas Daniels 24-Feb-13 12:39pm    
Where is <small>myImage</small> declared?
defunktlemon 24-Feb-13 12:58pm    
it's not - I don't know what to declare it as. A bitmap - but the image is drawn on background so does that make it a Bitmap, I thought not - I thought if background panel image it was just held in memory.
defunktlemon 24-Feb-13 13:07pm    
I decalred it as Bitmap myImage;
All errors left except one - 'use of unasigned local variable myImage'

or should it be - Bitmap myImage = panel1.BackgroundImage(Image.FromFile);
GrooverFromHolland 24-Feb-13 13:37pm    
You are almost there.
MyImage = Image.FromFile(openFileDialog1.FileName);
panel1.BackgroundImage = MyImage;
defunktlemon 24-Feb-13 13:48pm    
yes - that has gotten rid of the red. thank you Groover - I have one more error and then I think I can fly :)
for (int j = 0; j < myImage.Width; j++)
{
value += myImage.GetPixel(j, i).GetBrightness();//get colour of pixel and that Hue-Saturation-Brightness values
}
On the GetPixel i'm asked if i'm missing a using directive or an assembly reference - isn't this normally if a namespace is missing?

Add a declaration:
C#
Image myImage = panel1.BackgroundImage;
And put it after your if condition (or expand the if to encompass the remaining code - you only want to execute it if the user presses "OK" after all. I would also recommend that your OpenFileDialog is constructed in the method, and you use a Bitmap as the generic Image does not have a GetPixel method.
C#
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "JPEG IMAGES|*.jpg";
openFileDialog1.InitialDirectory = "C:\\Users\\jason\\Documents\\IProject\\code\\imageAlign\\imageAlign\\bin\\Debug";
openFileDialog1.Title = "Open Image";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    Bitmap myImage = (Bitmap)Image.FromFile(openFileDialog1.FileName);
    panel1.BackgroundImage = myImage;
    panel1.Invalidate();
    // Array for keeping the sums of each row of pixels
    float[] resultArray = new float[myImage.Height];

    // Populate the array with data from each row of pixels, using the brightness value
    for (int i = 0; i < myImage.Height; i++)
        {
        float value = 0;
        for (int j = 0; j < myImage.Width; j++)
            {
            value += myImage.GetPixel(j, i).GetBrightness();//get colour of pixel and that Hue-Saturation-Brightness values
            }
        resultArray[i] = value;
        }
    }
 
Share this answer
 
Comments
defunktlemon 25-Feb-13 8:41am    
Hi OriginalGriff.
If I add your declaration 'Image', I am then declaring a local variable already defined in that scope; as an Image and a Bitmap.
If I comment out the Bitmap declaration it seems ok - but with both of these methods the error on GetPixel (missing using directive or an assembly reference) reappears.

This is how the code looks, which I think is ok. It is after the 'If'statment. Is it wrong? I need the Image declaration as well?
Bitmap myImage;
myImage = (Bitmap)Image.FromFile(openFileDialog1.Filename);

Also, I can't understand your last comment -
"I would also recommend that your OpenFileDialog is constructed in the method, and you use a Bitmap as the generic Image does not have a GetPixel method."
OpenFileDialog is actioned after buttonClick1


panel1.BackgroundImage = myImage;
OriginalGriff 25-Feb-13 9:10am    
The original code you show in your question does not declare "myImage" - in fact the error you quote is there because no variable exists with that name: that is what the error message "'myImage' does not exist in the current context" means.

Image is a base class (it's actually abstract, but don't worry about that) from which Bitmap is derived, and the Bitmap class extends the Image class in many ways - one of which is the addition of the GetPixel method, which does not exist in the base Image class. So if you want to use GetPixel, you should declare the variable as a Bitmap, not an Image. (Otherwise you have to cast it each time, which is messy)
Once you have done that, GetPixel becomes available, returning a Color value, which has a GetBrightness method.

Your original code does not show the declaration of openFileDialog1 - so there is no automatic mechanism for creating a new OpenFileDialog instance each time. It is a much better idea to create a new one with a scope local to your method, as some forms cannot be re-displayed once they have been closed (as their resources are disposed when they close). This doesn't happen with OpenFileDialog (you can re-display it without any problems) but it's a good habit to get into - unless you need to preserve the info in a form, create it, show it, bin it, and don't let it exist outside the method.

I have managed to find a solution myself :)

The myImage image needed to be cast in Bitmap form

Solution:
C#
Bitmap myImage;
myImage = (Bitmap)Image.FromFile(openFileDialog1.Filename);
panel1.BackgroundImage = myImage;


Thanks all
And thanks me too :)
 
Share this answer
 

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