Click here to Skip to main content
15,900,530 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys;

First of all i'm new about c# and i found some codes for inverting colors of jpg images for a file around the internet.

But all i need is a exe file that inverting all jpg images in a specific folder

Can you help me about it please? I'm using VS2013 ultimate...
Posted
Updated 11-Feb-14 0:38am
v2

If you have code to invert a particular image, then you can simply wrap the calls to this in some simple path operations. Something like this might be appropriate:
C#
public void InvertAllImagesInFolder(string folderName)
{
  if (string.IsNullOrWhitespace(folderName)) throw new ArgumentException("You must supply the folder name");
  if (!Directory.Exists(folderName)) return; // Don't bother going any further - the folder doesn't exists.
  string[] files = Directory.GetFiles(folderName, "*.jpg")
  foreach (string file in files)
  {
    ProcessImage(file); // Call the image invert method.
  }
}
 
Share this answer
 
I hope this helps.

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            // image folder
            string path = @"d:\tmp\16";

            // for all files
            foreach (var f in Directory.GetFiles(path, "*.jpg"))
            {
                // read bitmap
                using (Bitmap b = Bitmap.FromFile(f) as Bitmap)
                {
                    // process bitmap
                    using (Bitmap b2 = this.ProcessBitmap(b))
                    {
                        // save new bitmap
                        b2.Save(Path.Combine(path, "pp_" + Path.GetFileName(f)), ImageFormat.Jpeg);
                    }
                }
            }

            foreach (var f in Directory.GetFiles(path, "pp_*.jpg"))
            {
                string fn = f.Replace("pp_", "");
                File.Delete(fn);
                File.Move(f, fn);
            }        
        }

        // some operation 
        private Bitmap ProcessBitmap(Bitmap b)
        {
            // new bitmap
            Bitmap b2 = new Bitmap(b);
            // traverse all pixels
            for (int i = 0; i < b2.Width; i++)
            {
                for (int j = 0; j < b2.Height; j++)
                {
                    // get original color
                    Color c = b2.GetPixel(i, j);
                    // modify pixel
                    b2.SetPixel(i, j, Color.FromArgb(c.A, 255 - c.R, 255 - c.G, 255 - c.B));
                }
            }
            return b2;
        }
    }
}
 
Share this answer
 
v2
Comments
Member 10587827 11-Feb-14 8:40am    
thanks it worked but this code creates new jpg files...is it possible to do it without creating new jpg files?
Vedat Ozan Oner 11-Feb-14 9:04am    
ok, updated the solution
Pete O'Hanlon 11-Feb-14 9:10am    
Not bad, but that is going to be slow processing. GDI iteration using GetPixel and SetPixel is notoriously slow.
Member 10587827 11-Feb-14 9:12am    
Yes Pete u're right...it's so slow..is there any way to make it faster? Meanwhile thank you Vedat.
Vedat Ozan Oner 11-Feb-14 9:19am    
yes, it is slow. but you can access pixel values directly in an 'unsafe' manner. there is a good example here: http://bobpowell.net/lockingbits.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900