且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何更改像素彩色图像的C#.NET

更新时间:2021-07-17 09:37:46

下面是解决方案我已经做了与像素。

Here is the Solution I have done with Pixels.

附加源$ C ​​$ C所以可以尝试准确,得到的结果。

Attaching the source code so one can try the exact and get the result.

我有一个128×128(宽×高)的样本图像。

I have sample images of 128x128 (Width x Height).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
//using System.Globalization;

namespace colorchange
{
   class Program
   {
      static void Main(string[] args)
      {
          try
          {
              ImageHandler ih = new ImageHandler();
              Bitmap bmp = null;
              //The Source Directory in debug\bin\Big\
              string[] files = Directory.GetFiles("Big\\");
              foreach (string filename in files)
              {
                 bmp = (Bitmap)Image.FromFile(filename);                    
                 bmp = ChangeColor(bmp);
                 string[] spliter = filename.Split('\\');
                 //Destination Directory debug\bin\BigGreen\
                 bmp.Save("BigGreen\\" + spliter[1]);
              }                                                 
           }
           catch (System.Exception ex)
           {
              Console.WriteLine(ex.ToString());
           }            
       }        
       public static Bitmap ChangeColor(Bitmap scrBitmap)
       {
          //You can change your new color here. Red,Green,LawnGreen any..
          Color newColor = Color.Red;
          Color actulaColor;            
          //make an empty bitmap the same size as scrBitmap
          Bitmap newBitmap = new Bitmap(scrBitmap.Width, scrBitmap.Height);
          for (int i = 0; i < scrBitmap.Width; i++)
          {
             for (int j = 0; j < scrBitmap.Height; j++)
             {
                //get the pixel from the scrBitmap image
                actulaColor = scrBitmap.GetPixel(i, j);
                // > 150 because.. Images edges can be of low pixel colr. if we set all pixel color to new then there will be no smoothness left.
                if (actulaColor.A > 150)
                    newBitmap.SetPixel(i, j, newColor);
                else
                    newBitmap.SetPixel(i, j, actulaColor);
             }
          }            
          return newBitmap;
       }
   }
}

//下面是样本图像和不同的结果通过应用不同的颜色 如何更改像素彩色图像的C#.NET

//Below is the sample image and different results by applying different color

code修改将是非常美联社preciated。

Code modifications will be highly appreciated.