当使用我的轨迹条调整亮度时,得到意想不到的异常

本文关键字:意想不到 异常 亮度 我的 轨迹 调整 | 更新日期: 2023-09-27 18:07:09

专家们好,我将通过选择特定的文件夹从两个组合框下拉(例如15-20个图像)中获取图像到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;

        }
    }
}

当使用我的轨迹条调整亮度时,得到意想不到的异常

您混淆了一个字段和一个局部变量:

字段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());)但总是通过调节亮度的方法:

pictureBox1.Image = adjustbrightness(Image.FromFile(alist[0].ToString()), 255);

并删除Bitmap newbitmap;