How to Change Pixel Color of an Image in C#.NET

前端 未结 4 1178
死守一世寂寞
死守一世寂寞 2020-11-29 06:05

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

The problem is, Now I have be

4条回答
  •  醉酒成梦
    2020-11-29 06:35

    I will give you another solution since this does not calculate for every pixel.

    Its short and simple. Convert time is 62 ms:

    public Bitmap Color(Bitmap original)
            {
                //create a blank bitmap the same size as original
                Bitmap newBitmap = new Bitmap(original.Width, original.Height);
    
                //get a graphics object from the new Image
                Graphics g = Graphics.FromImage(newBitmap);
    
                //create the color you want ColorMatrix
                //now is set to red, but with different values 
                //you can get anything you want.
                ColorMatrix colorMatrix = new ColorMatrix(
                    new float[][]
                    {
    
                        new float[] {1f, .0f, .0f, 0, 0},
                        new float[] {1f, .0f, .0f, 0, 0},
                        new float[] {1f, .0f, .0f, 0, 0},
                        new float[] {0, 0, 0, 1, 0},
                        new float[] {0, 0, 0, 0, 1}
                    });
    
                //create some image attributes
                ImageAttributes attributes = new ImageAttributes();
    
                //set the color matrix attribute
                attributes.SetColorMatrix(colorMatrix);
    
                //draw original image on the new image using the color matrix
                g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
                    0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
    
                //release sources used
                g.Dispose();
                return newBitmap;
            }
    

提交回复
热议问题