How do you programmatically edit .png?

I have 28 images, each of which has 3 sizes (84 in total), which are monochrome with different alpha layers to create each image. I want to make each of them available in 5 different colors. it will be 420 images. Obviously, it will be a huge pain to do it manually. I don't have Photoshop, so any type of photoshop function is not a valid answer. I have Paint.NET, but adjusting the hue does not work for me, because changing the hue alone does not give me the colors I want.

Basically, I need to do for each pixel in the image, take the RGBA value and replace the RGB with the new RGB value and keep the same value A.

Does anyone know how to do this? I was not lucky to find on StackOverflow or Google (possibly using the wrong search terms).

I would prefer an answer in C # or VB.NET, but if anyone knows how to do this in any language, maybe I can apply it to C # or VB.NET.

- Edit -

In case someone finds this and is looking for an answer, here is what I got based on the link from Yorye Nathan.

private const int RED = 51; private const int GREEN = 181; private const int BLUE = 229; private const int NEW_RED = 170; private const int NEW_GREEN = 102; private const int NEW_BLUE = 204; private void Form1_Load(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image OriginalImage = Image.FromFile(openFileDialog1.FileName); Image NewImage = ColorFilter(OriginalImage); pictureBox1.Image = OriginalImage; pictureBox2.Image = NewImage; } } public static Image ColorFilter(Image originalImage) { Bitmap newImage = new Bitmap(originalImage); BitmapData originalData = (originalImage as Bitmap).LockBits(new Rectangle(0, 0, originalImage.Width, originalImage.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); BitmapData newData = (newImage as Bitmap).LockBits(new Rectangle(0, 0, originalImage.Width, originalImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int originalStride = originalData.Stride; System.IntPtr originalScan0 = originalData.Scan0; int newStride = newData.Stride; System.IntPtr newScan0 = newData.Scan0; unsafe { byte* pOriginal = (byte*)(void*)originalScan0; byte* pNew = (byte*)(void*)newScan0; int nOffset = originalStride - originalImage.Width * 4; byte red, green, blue; for (int y = 0; y < originalImage.Height; ++y) { for (int x = 0; x < originalImage.Width; ++x) { blue = pOriginal[0]; green = pOriginal[1]; red = pOriginal[2]; if (pOriginal[0] == BLUE && pOriginal[1] == GREEN && pOriginal[2] == RED) { pNew[0] = (byte)NEW_BLUE; pNew[1] = (byte)NEW_GREEN; pNew[2] = (byte)NEW_RED; } pOriginal += 4; pNew += 4; } pOriginal += nOffset; pNew += nOffset; } } (originalImage as Bitmap).UnlockBits(originalData); (newImage as Bitmap).UnlockBits(newData); return newImage; } 
+6
source share
1 answer

Check the question . Change slightly with replacing pixel bits so that they add them instead of replacing them, and you're good to go.

+4
source

Source: https://habr.com/ru/post/911631/


All Articles