C# 中的图像着色
Image Coloring In C#
我想用 C# 为图像重新着色以保留真实图像(就像我们在任何照片编辑器中所做的那样)。我正在尝试这样做但没有用。任何人都可以在这方面帮助我......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace Images
{
class Program
{
static void Main(string[] args)
{
try
{
Bitmap img = new Bitmap(@"D:\Image\Chrysanthemum.jpg");
Bitmap NewImage = new Bitmap(img,img.Width,img.Height);
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
{
NewImage.SetPixel(i, j,Color.FromArgb(0,0,240,0));
}
}
}
NewImage.MakeTransparent();
NewImage.Save(@"D:\Image",System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception exc)
{
Console.Write(exc);
}
}
}
}
如果您想进行颜色调制,正确的方法是遍历图像的每个像素,将每个 RGB
值表示为 float
从 0.0f
到 1.0f
,然后 将其乘以 您想要的颜色。
using System.Drawing;
namespace stuff
{
class Program
{
static void Main(string[] args)
{
Bitmap pImage = new Bitmap(@"C:\Users\...\Desktop\test.jpg");
Bitmap pModified = new Bitmap(pImage.Width, pImage.Height);
Color tintColor = Color.FromArgb(255, 0, 0);
for (int x = 0; x < pImage.Width; x++)
{
for (int y = 0; y < pImage.Height; y++)
{
//Calculate the new color
var oldColor = pImage.GetPixel(x, y);
byte red =(byte)(256.0f * (oldColor.R / 256.0f) * (tintColor.R / 256.0f));
byte blue = (byte)(256.0f * (oldColor.B / 256.0f) * (tintColor.B / 256.0f));
byte green = (byte)(256.0f * (oldColor.G / 256.0f) * (tintColor.G / 256.0f));
Color newColor = Color.FromArgb(red, blue, green);
pModified.SetPixel(x, y, newColor);
}
}
pModified.Save(@"C:\Users\...\Desktop\tint.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
}
}
输入:
输出:
您所做的只是创建与原始图像大小相同的第二张图像。第二张图片填充了绿色(但没有 alpha - 所以它只是透明的)。我怀疑您希望 MakeTransparent()
方法对原始图像重新着色,但事实并非如此。这种方法只会使某种颜色的像素透明。这不是图像的不透明度。
所以这是两张图片 - 一张挨着一张,它们彼此无关(除了大小相同)。
因此您可以通过从您的 img
创建一个 Graphics
对象来直接操作 img
变量中的图像(您可以复制它以保留原始图像)可以在图像上绘制半透明颜色。
using (var gfx = Graphics.FromImage(img))
{
using (var brush = new SolidBrush(MYCOLOR)
gfx.FillRect(brush, MYRECT)
}
但我强烈建议您使用图像处理库 来实现您想要实现的图像处理。所以请检查免费 imageprocessor.org. Their hue filter 可能是你想要的。
我想用 C# 为图像重新着色以保留真实图像(就像我们在任何照片编辑器中所做的那样)。我正在尝试这样做但没有用。任何人都可以在这方面帮助我......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace Images
{
class Program
{
static void Main(string[] args)
{
try
{
Bitmap img = new Bitmap(@"D:\Image\Chrysanthemum.jpg");
Bitmap NewImage = new Bitmap(img,img.Width,img.Height);
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
{
NewImage.SetPixel(i, j,Color.FromArgb(0,0,240,0));
}
}
}
NewImage.MakeTransparent();
NewImage.Save(@"D:\Image",System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception exc)
{
Console.Write(exc);
}
}
}
}
如果您想进行颜色调制,正确的方法是遍历图像的每个像素,将每个 RGB
值表示为 float
从 0.0f
到 1.0f
,然后 将其乘以 您想要的颜色。
using System.Drawing;
namespace stuff
{
class Program
{
static void Main(string[] args)
{
Bitmap pImage = new Bitmap(@"C:\Users\...\Desktop\test.jpg");
Bitmap pModified = new Bitmap(pImage.Width, pImage.Height);
Color tintColor = Color.FromArgb(255, 0, 0);
for (int x = 0; x < pImage.Width; x++)
{
for (int y = 0; y < pImage.Height; y++)
{
//Calculate the new color
var oldColor = pImage.GetPixel(x, y);
byte red =(byte)(256.0f * (oldColor.R / 256.0f) * (tintColor.R / 256.0f));
byte blue = (byte)(256.0f * (oldColor.B / 256.0f) * (tintColor.B / 256.0f));
byte green = (byte)(256.0f * (oldColor.G / 256.0f) * (tintColor.G / 256.0f));
Color newColor = Color.FromArgb(red, blue, green);
pModified.SetPixel(x, y, newColor);
}
}
pModified.Save(@"C:\Users\...\Desktop\tint.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
}
}
输入:
输出:
您所做的只是创建与原始图像大小相同的第二张图像。第二张图片填充了绿色(但没有 alpha - 所以它只是透明的)。我怀疑您希望 MakeTransparent()
方法对原始图像重新着色,但事实并非如此。这种方法只会使某种颜色的像素透明。这不是图像的不透明度。
所以这是两张图片 - 一张挨着一张,它们彼此无关(除了大小相同)。
因此您可以通过从您的 img
创建一个 Graphics
对象来直接操作 img
变量中的图像(您可以复制它以保留原始图像)可以在图像上绘制半透明颜色。
using (var gfx = Graphics.FromImage(img))
{
using (var brush = new SolidBrush(MYCOLOR)
gfx.FillRect(brush, MYRECT)
}
但我强烈建议您使用图像处理库 来实现您想要实现的图像处理。所以请检查免费 imageprocessor.org. Their hue filter 可能是你想要的。