我正在Java中处理图像,我设计了超过100多个图像(.png)格式,它们都是透明和黑色绘图。
问题是,现在我被要求更改绘图的颜色(黑色 - )。
我在谷歌上搜索了许多代码,这些代码改变了图像的位图(像素),但我不猜测我必须做什么来匹配确切的像素,并在图像处于透明模式时特别替换。
下面是.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;
但根本不匹配,我调试了一下,整个文件中,没有Color(originalColor)变量的Red,Green,Blue参数值。
有人可以帮忙吗?
这是我用像素完成的解决方案。
附上源代码,以便人们可以准确地尝试并获得结果。
我有 128x128(宽 x 高)的示例图像。
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
{
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 actualColor;
//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
actualColor = 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 (actualColor.A > 150)
newBitmap.SetPixel(i, j, newColor);
else
newBitmap.SetPixel(i, j, actualColor);
}
}
return newBitmap;
}
}
}
//Below is the sample image and different results by applying different color
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)