如何更改像素彩色图像的C#.NET [英] How to Change Pixel Color of an Image in C#.NET

查看:209
本文介绍了如何更改像素彩色图像的C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java图像时,我已经设计了超过100 +的图像文件(.png)格式,他们都是Trasparent和黑色绘图。

I am working with Images in Java, I have designed more over 100+ images(.png) format, They were all Trasparent and Black Color Drawing.

问题是,现在我被要求改变图纸(黑色-to)。

The problem is, Now I have been asked to change the color of the Drawing (Black -to ).

我已经搜查了许多code剪断,在谷歌,改变图像的位图(像素),但我没有猜我有什么做匹配的确切像素并更换专门的图像时,如果透明模式。 下面是code在.net(C#)

I have searched many code snipped at google,that changes the Bitmap (pixels) of the Image, but i am not guessing what i have to do to match the exact pixel and replace specially when the images if in Transparent mode. Below is the code in .Net (C#)

        Bitmap newBitmap = new Bitmap(scrBitmap.Width, scrBitmap.Height);
        for (int i = 0; i < scrBitmap.Width; i++)
        {
            for (int j = 0; j < scrBitmap.Height; j++)
            {                    
                originalColor = scrBitmap.GetPixel(i, j);
                if(originalColor = Color.Black)
                  newBitmap.SetPixel(i, j, Color.Red);
            }
        }            
        return newBitmap;

但它不匹配可言,我调试它,在整个文件中,有红色,绿色,颜色(originalColor)可变的蓝参数。

but it was not matching at all, I debugged it, throughout the file, there was no value of Red,Green,Blue parameters of Color (originalColor) variable.

任何人都可以帮忙吗?

推荐答案

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

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

//下面是样本图像和不同的结果通过应用不同的颜色

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

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

Code modifications will be highly appreciated.

这篇关于如何更改像素彩色图像的C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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