命名背景面板图像 [英] naming background panel image
问题描述
嗨。
我在后台面板中打开了一张图片。我想得到像素的颜色和Hue-Saturation-Brightness值。我有一个代码在我的程序的另一部分完成了这个但我不确定在后台面板中打开时我的图像是什么。我收到错误消息,说当前上下文中不存在名称''myImage'。
thanks
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 = 打开图像;
if (openFileDialog1.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
panel1.BackgroundImage = Image.FromFile(openFileDialog1.FileName);
panel1.Invalidate();
}
// 用于保持每行像素总和的数组
float [] resultArray = new float 跨度> [myImage.Height];
// 使用亮度值<填充来自每行像素的数据/ span>
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(); // 获取像素的颜色和色调饱和度-Brightness值
}
resultArray [i] = value ;
}
}
添加声明:
图片myImage = panel1.BackgroundImage;并将其放在
if
条件之后(或展开if
包含剩余的代码 - 如果用户按OK,你只想执行它。我还建议你在方法中构造OpenFileDialog,并使用Bitmap作为通用Image没有GetPixel方法。
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1 .Filter = JPEG IMAGES | * .jpg;
openFileDialog1.InitialDirectory = C:\\Users \\ jason \\Documents \\IProject \\ code \\imageAlign\\imageAlign\\bin\\Debug;
openFileDialog1.Title = 打开图像;
if (openFileDialog1.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
Bitmap myImage =(位图)Image.FromFile(openFileDialog1.FileName);
panel1.BackgroundImage = myImage;
panel1.Invalidate();
// 用于保持每行像素总和的数组
float [] resultArray = new float [myImage。高度];
// 使用亮度值<填充来自每行像素的数据/ span>
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(); // 获取像素的颜色和色调饱和度-Brightness值
}
resultArray [i] = value ;
}
}
我自己找到了解决方案:)
需要以位图形式投射myImage图像
解决方案:
Bitmap myImage;
myImage =(Bitmap)Image.FromFile(openFileDialog1.Filename);
panel1.BackgroundImage = myImage;
谢谢所有
也谢谢我:)
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
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;
}
}
Add a declaration:
Image myImage = panel1.BackgroundImage;And put it after your
if
condition (or expand theif
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.
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; } }
I have managed to find a solution myself :)
The myImage image needed to be cast in Bitmap form
Solution:
Bitmap myImage; myImage = (Bitmap)Image.FromFile(openFileDialog1.Filename); panel1.BackgroundImage = myImage;
Thanks all
And thanks me too :)
这篇关于命名背景面板图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!