如何使用c#在特定文件夹中反转所有Jpg图像? [英] How Can I Invert All Jpg Images In A Specific Folder with using c#?

查看:71
本文介绍了如何使用c#在特定文件夹中反转所有Jpg图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们;



首先,我是关于c#的新手,我发现了一些用于反转互联网文件的jpg图像颜色的代码。 br />


但是我只需要一个exe文件来反转特定文件夹中的所有jpg图像



你能来吗请帮帮我吧?我正在使用VS2013终极...

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...

推荐答案

如果你有代码来反转特定的图像,那么你可以简单地将调用包装在一些简单的路径操作中。这样的事情可能是合适的:
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:
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.
  }
}


我希望这会有所帮助。



I hope this helps.

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


这篇关于如何使用c#在特定文件夹中反转所有Jpg图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆