简体   繁体   中英

Image Coloring In C#

I want to recolor an Image in C# such that it preserves the real image (just as we do in any Photo Editor). I am trying to do this but of no use. Can anybody help me in this regard...

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\1",System.Drawing.Imaging.ImageFormat.Jpeg);

           }
        catch(System.Exception exc)
        {
            Console.Write(exc);
        }
    }

   }
   }

If you want to do Color Modulation , the right approach would be to iterate through each pixel of the image, represent each RGB value as a float from 0.0f to 1.0f , then multiply it by the color you want it to have.

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

Input: 在

Output: 出

What you are doing is just creating a second image with the same size of the original one. This second image is filled with a green color (but no alpha - so it's just transparent). I suspect that you hoped for the method MakeTransparent() to recolor the original image but this is not the case. This method will just make pixels of a certain color transparent. This is not the image's opacity.

So these are two images - one beside the other, they have nothing to do with each other (besides of the same size).

So you could manipulate the image in the img variable directly (you may copy it to preserve the original one) by creating a Graphics object from your img with which you can draw a semi-transparent color over the image.

using (var gfx = Graphics.FromImage(img))
{
    using (var brush = new SolidBrush(MYCOLOR)
        gfx.FillRect(brush, MYRECT)
}

But I would heavily insist that you use a image processing library for the image manipulation you want to achieve. So please check the free imageprocessor.org . Their hue filter might be what you want to have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM