Hello Experts,我将通过选择特定文件夹从两个组合框下拉(例如15-20images)将图像输入到picturebox中。我想调整它们的亮度,所以当我移动轨迹栏调整亮度时,输出窗口会消失,显示错误消息。我正在使用C#使用VS2013-winforms。在此先感谢,我尝试过,
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Bitmap newbitmap;
ArrayList alist = new ArrayList();
int i = 0;
int filelength = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(@"C:\Users\Arun\Desktop\scanned");
DirectoryInfo[] folders = di.GetDirectories();
comboBox1.DataSource = folders;
}
private void button7_Click(object sender, EventArgs e)
{
if (i + 1 < filelength)
{
pictureBox1.Image = Image.FromFile(alist[i + 1].ToString());
i = i + 1;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
private void button8_Click(object sender, EventArgs e)
{
if (i - 1 >= 0)
{
pictureBox1.Image = Image.FromFile(alist[i - 1].ToString());
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
i = i - 1;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = comboBox1.SelectedItem.ToString();
String fullpath = Path.Combine(@"C:\Users\Arun\Desktop\scanned", selected);
DirectoryInfo di1 = new DirectoryInfo(fullpath);
DirectoryInfo[] folders1 = di1.GetDirectories();
comboBox2.DataSource = folders1;
}
private void button9_Click(object sender, EventArgs e)
{
string selected1 = comboBox1.SelectedItem.ToString();
string selected2 = comboBox2.SelectedItem.ToString();
//Initially load all your image files into the array list when form load first time
System.IO.DirectoryInfo inputDir = new System.IO.DirectoryInfo(Path.Combine(@"C:\Users\Arun\Desktop\scanned", selected1, selected2)); //Source image folder path
try
{
if ((inputDir.Exists))
{
alist.Clear();
//Get Each files
System.IO.FileInfo file = null;
foreach (System.IO.FileInfo eachfile in inputDir.GetFiles())
{
file = eachfile;
if (file.Extension == ".tif")
{
alist.Add(file.FullName); //Add it in array list
//filelength = filelength + 1;
filelength = alist.Count;
}
else if(file.Extension == ".jpg")
{
alist.Add(file.FullName); //Add it in array list
// filelength = filelength + 1;
filelength = alist.Count;
}
}
pictureBox1.Image = Image.FromFile(alist[0].ToString()); //Display intially first image in picture box as sero index file path
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
i = 0;
}
}
catch (Exception ex)
{
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
label2.Text = trackBar1.Value.ToString();
pictureBox1.Image = adjustbrightness (newbitmap, trackBar1.Value);
}
public static Bitmap adjustbrightness(Bitmap image,int value)
{
Bitmap tempbitmap = image;
float finalvalue = (float)value / 255.0f;
Bitmap newbitmap = new Bitmap(tempbitmap.Width, tempbitmap.Height);
Graphics newgraphics = Graphics.FromImage(newbitmap);
float[][] floatcolormatrix = {
new float[] {1,0,0,0,0},
new float[] {0,1,0,0,0},
new float[] {0,0,1,0,0},
new float[] {0,0,0,1,0},
new float[] {finalvalue,finalvalue,finalvalue,1,1}
};
ColorMatrix newcolormatrix = new ColorMatrix(floatcolormatrix);
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(newcolormatrix);
newgraphics.DrawImage(tempbitmap, new Rectangle(0, 0, tempbitmap.Width, tempbitmap.Height), 0, 0, tempbitmap.Width, tempbitmap.Height, GraphicsUnit.Pixel, attributes);
attributes.Dispose();
newgraphics.Dispose();
return newbitmap;
}
}
}
答案 0 :(得分:0)
你混淆了一个字段和一个局部变量:
永远不会分配字段Bitmap newbitmap
。
pictureBox1.Image = adjustbrightness (newbitmap, trackBar1.Value);
方法adjustbrightness
将在访问此处的属性时崩溃:
public static Bitmap adjustbrightness(Bitmap image,int value)
{
Bitmap tempbitmap = image;
float finalvalue = (float)value / 255.0f; // <- this should probably be 256.0f
Bitmap newbitmap = new Bitmap(tempbitmap.Width, tempbitmap.Height); // <-------
Graphics newgraphics = Graphics.FromImage(newbitmap);
永远不会分配传递的参数image
。
要解决此问题,您不应将位图直接加载到图片框中。 (pictureBox1.Image = Image.FromFile(alist[0].ToString());
)但总是通过adjustbrightness方法:
pictureBox1.Image = adjustbrightness(Image.FromFile(alist[0].ToString()), 255);
然后移除字段Bitmap newbitmap;