Hello,

im trying to make a program that opens up jpeg files one at a time and look at there brightness and then if its past a threashold moves the file to a "keep" folder if not moves it to a "varify" folder

im new to C# so im not sure where to start

sorry if this has been asked before

well here is the code i have at the moment

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Threading.Tasks;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e);
        private void CompareImages(string sourceFolder, string disposedImgFolder, double threshold, string[] extensions)
        {
            if (Directory.Exists(sourceFolder))
            {
                DirectoryInfo dir = new DirectoryInfo(sourceFolder);
                List<FileInfo> pictures = new List<FileInfo>();

                foreach (string ext in extensions)
                {
                    FileInfo[] fi = dir.GetFiles(ext);
                    pictures.AddRange(fi);
                }

               
                Directory.CreateDirectory(disposedImgFolder);

                int j = 0;

                if (pictures.Count > 0)
                {
                    int count = pictures.Count;
                    
                    Parallel.For(0, count, i =>
                    {
                        Image img = null;
                        Bitmap bmp = null;

                        try
                        {
                            
                            img = Image.FromFile(pictures[i].FullName);
                            bmp = new Bitmap(img);
                            img.Dispose();

                            double avg = GetAveragePixelValue(bmp);

                            bmp.Dispose();

                            if (avg < threshold)
                            {
                                string dest = Path.Combine(disposedImgFolder, pictures[i].Name);

                                if (File.Exists(dest) == false)
                                {
                                    pictures[i].MoveTo(dest);
                                    j++;
                                }
                                else
                                {
                                    
                                }
                            }
                            else
                            {

                            }
                        }
                        catch
                        {
                            if (img != null)
                                img.Dispose();
                            if (bmp != null)
                                bmp.Dispose();
                        }
                    });

                    MessageBox.Show("Done, " + j.ToString() + " files moved.");
                }
            }
        }

        private double GetAveragePixelValue(Bitmap bmp)
        {
            throw new NotImplementedException();
        }
    }
}

how do i define what the threashold is?

also i get an error

Error 1 'WindowsFormsApplication1.Form1.button1_Click(object, System.EventArgs)' must declare a body because it is not marked abstract, extern, or partial C:\Users\windows -7\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 23 22 WindowsFormsApplication1

any idea why?

as i cant edit my other post i have changed the code to this;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
             private void button1_Click(object sender, EventArgs e)
        {
            CompareImages(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "ffff"), 127.0, new string[] {"*.jpg", "*.png"});
        }

        //extension should be complete search strings like "*.png"
        //you might want to implement this in a second thread and display some progress
        //this will compare the images overall avg brightness and move the image, if the overall brightness
        //is smaller than the threshold
        private void CompareImages(string sourceFolder, string disposedImgFolder, double threshold, string[] extensions)
        {
            if (Directory.Exists(sourceFolder))
            {
                DirectoryInfo dir = new DirectoryInfo(sourceFolder);
                List<FileInfo> pictures = new List<FileInfo>();

                foreach (string ext in extensions)
                {
                    FileInfo[] fi = dir.GetFiles(ext);
                    pictures.AddRange(fi);
                }

                //throws no error if already exists
                Directory.CreateDirectory(disposedImgFolder);

                int j = 0;

                if (pictures.Count > 0)
                {
                    for (int i = 0; i < pictures.Count; i++)
                    {
                        Image img = null;
                        Bitmap bmp = null;

                        try
                        {
                            //load the image and make a copy, so that the file could be moved
                            img = Image.FromFile(pictures[i].FullName);
                            bmp = new Bitmap(img);
                            img.Dispose();

                            double avg = GetAveragePixelValue(bmp);

                            bmp.Dispose();

                            //compare
                            if (avg < threshold)
                            {
                                string dest = Path.Combine(disposedImgFolder, pictures[i].Name);

                                if (File.Exists(dest) == false)
                                {
                                    pictures[i].MoveTo(dest);
                                    j++;
                                }
                                else
                                {
                                    //do whatever you want to do, if the file already exists
                                }
                            }
                            else
                            {
                                //do whatever
                            }
                        }
                        catch
                        {
                            if (img != null)
                                img.Dispose();
                            if (bmp != null)
                                bmp.Dispose();
                        }
                    }

                    MessageBox.Show("Done, " + j.ToString() + " files moved.");
                }
            }
        }

        private unsafe double GetAveragePixelValue(Bitmap bmp)
        {
            BitmapData bmData = null;

            try
            {
                bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                int stride = bmData.Stride;
                IntPtr scan0 = bmData.Scan0;
                int w = bmData.Width;
                int h = bmData.Height;

                double sum = 0;
                long pixels = bmp.Width * bmp.Height;

                byte* p = (byte*)scan0.ToPointer();

                for (int y = 0; y < h; y++)
                {
                    p = (byte*)scan0.ToPointer();
                    p += y * stride;

                    for (int x = 0; x < w; x++)
                    {
                        //get the average of the current pixel (structure for bmps is b-g-r-a;
                        //so we GetAccessibilityObjectById only the first, second and third byte and divide by 3)
                        double i = ((double)p[0] + p[1] + p[2]) / 3.0;
                        sum += i;

                        //increment by pixelSize
                        p += 4;
                    }

                    //no offset incrementation needed when getting 
                    //the pointer at the start of each row
                }

                bmp.UnlockBits(bmData);

                double result = sum / (double)pixels;

                return result;
            }
            catch
            {
                try
                {
                    bmp.UnlockBits(bmData);
                }
                catch
                {

                }
            }

            return -1;
        }
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.