Ich spiele mit dem Austauschen von Farbwerten eines Bildes, aber ich habe etwas gefunden, das ich nicht ganz verstehe - und ich kann kein gutes finden Lesen Sie weiter auf Google in der Sache. Ich kann die Farben eines Bildes vertauschen, aber es verändert auch die Größe der Ausgabedatei im Vergleich zur Größe der Eingabedatei.Dateigröße wird nach dem Austauschen von Farbwerten in Bitmap
Nachfolgend finden Sie eine Testklasse die ich geschrieben habe, die Angelegenheit zu prüfen, und was es tut, um es zusammenzufassen ist:
- die Bitmap-Speicher zuweisen.
- Erstellen Sie ein Array von RGB-Werten.
- Teilen Sie das RGB-Array in drei separate Arrays auf (r, g und b).
- Swap alle Werte in rot (r [0] < -> r [1], r [2] < -> r [3], etc.)
- die drei Arrays bei und an die Anordnung von RGB-Werte zuweisen .
- In Bitmap zurückkopieren.
- Geben Sie den zugewiesenen Speicher frei.
- Exportieren Sie die Datei.
-Code ist wie unten zu sehen:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace GraphTheory
{
class Test
{
public Test(Bitmap bmp)
{
#region Assign bitmap to memory
// Rectangle to hold the bmp.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
// Lock the bitmap to the rectangle/system memory.
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
// Get the adress of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgb = new byte[bytes];
// Copy the RGB values of the bitmap into the array.
Marshal.Copy(ptr, rgb, 0, bytes);
#endregion
#region Split rgb array into three arrays
// Number of colors in the image.
int colors = bytes/3;
// Declare three arrays to hold the RGB values of the bitmap.
byte[] r = new byte[colors];
byte[] g = new byte[colors];
byte[] b = new byte[colors];
// Set starting pos of color index.
int colorIndex = 0;
// Split the array of RGB values into three seperate arrays.
for (int i = 0; i < rgb.Length; i += 3)
{
int j = i + 1, k = i + 2;
r[colorIndex] = rgb[k];
g[colorIndex] = rgb[j];
b[colorIndex] = rgb[i];
colorIndex++;
}
#endregion
#region Hide data in the colors of the bitmap
for (int i = 0; i < colors; i += 2)
{
switchBits(ref r[i], ref r[i + 1]);
}
#endregion
#region Join the three arrays into one rgb array
// Reset color index.
colorIndex = 0;
// Replace the values of the rgb array with the values of the r, g and b arrays.
for (int i = 0; i < rgb.Length; i += 3)
{
int j = i + 1, k = i + 2;
rgb[k] = r[colorIndex];
rgb[j] = g[colorIndex];
rgb[i] = b[colorIndex];
colorIndex++;
}
#endregion
#region Free bitmap from memory and save to file
// Copy the RGB values back to the bitmap
Marshal.Copy(rgb, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
// Export the image.
bmp.Save("../../output.png");
#endregion
}
private void switchBits(ref byte bit1, ref byte bit2)
{
byte tmp = bit1;
bit1 = bit2;
bit2 = tmp;
}
}
}
ich einfach nicht verstehen, warum das die Bildgröße der Bitmap ändern würde, da ich keine Farbwert am ersetzen, so dass sie nur neu anordnen.
Größe der Eingabedatei: [884 KB]
Größe der Ausgabedatei: [1335 KB]
No das Bild nicht einen Alphakanal enthält:
Image.IsAlphaPixelFormat(image.PixelFormat) == false
In welchem Format sind die Ein- und Ausgabedateien: PNG wird standardmäßig eine alpha.Try dieses retten? – Reti43
Sowohl Eingang als auch Ausgang haben das Format .png. Ich lade die Bitmap wie folgt: 'Bitmap bmp = new Bitmap (" ../../ input.png ");' und exportiere sie als 'bmp.Save (" ../../ output.png ") ; '@ Reti43 –
Und wenn Sie die Eingabedatei laden und sofort ohne Änderung speichern, wird es die gleiche Größe wie das Original haben? – Reti43