-1

Hello Experts, I will be getting images into picturebox from two comboboxes dropdown(say 15-20images) by selecting specific folders.I want to adjust their brightness, so when i move the trackbar to adjust brightness the output window fades away and displays a error message.I am working with VS2013-winforms with C#. Thanks in advance, I have tried with,

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;


        }

    }
}
leppie
  • 115,091
  • 17
  • 196
  • 297
warriors
  • 1
  • 1
  • 6
  • Can you please provide the specific error? As well as the stack trace, if you have one? – Thumper Sep 16 '16 at 05:59
  • The error message is probably `IndexOutOfBoundsException`? – Jeroen van Langen Sep 16 '16 at 05:59
  • An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe Additional information: Object reference not set to an instance of an object. @Thumper – warriors Sep 16 '16 at 06:02
  • Instead of using `filelength`, you should use `alist.Count`. – Jeroen van Langen Sep 16 '16 at 06:03
  • **1)** You never assigned anything to `newbitmap` before passing it to `adjustbrightness` method, so as a result `tempbitmap` is null and `tempbitmap.Width` will throw a `NullReferenceException`. **2)** Always when posting question include the exception type and exception message and show the line of code which you receive exception there. **3)** Make sure you use break point and debugger enough to find the reason of exception yourself. [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Reza Aghaei Sep 16 '16 at 07:31

1 Answers1

0

You have mixed up a field and a local variable:

the field Bitmap newbitmap never gets assigned.

 pictureBox1.Image = adjustbrightness (newbitmap, trackBar1.Value);

The method adjustbrightness will crash on accessing the properties here:

    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);

The parameter image that is passed, is never assigned.

To fix this, you shouldn't load the bitmap directly into the picturebox. (pictureBox1.Image = Image.FromFile(alist[0].ToString());) But always via the adjustbrightness method:

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

And remove the field Bitmap newbitmap;

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57