Tôi biết điều này đã được trả lời, nhưng dựa trên Hans passant của trả lời các mã kết quả trông giống như sau:
public class Recolor
{
public static Bitmap Tint(string filePath, Color c)
{
// load from file
Image original = Image.FromFile(filePath);
original = new Bitmap(original);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(original);
//create the ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {c.R/255.0f,
c.G/255.0f,
c.B/255.0f,
0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the 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);
//dispose the Graphics object
g.Dispose();
//return a bitmap
return (Bitmap)original;
}
}
Tải về một bản demo làm việc ở đây: http://benpowell.org/change-the-color-of-a-transparent-png-image-icon-on-the-fly-using-asp-net-mvc/
Điều này không đặt màu của từng pixel thành một màu cụ thể, phải không? Tôi khá chắc chắn rằng sẽ tăng từng kênh màu bằng R, G & B. Tôi muốn toàn bộ hình ảnh là một màu đồng nhất, trong khi vẫn giữ được độ trong suốt/alpha của mỗi pixel. – Charles
Các số không trong đường chéo tạo ra màu đen, các số dưới cùng được thêm vào. –
Ah-ha. Tôi đã không nghĩ hết. Tôi cá rằng nó sẽ hoạt động hoàn hảo. Tôi sẽ kiểm tra và quay lại ngay với bạn. – Charles