我與交換的圖像的色值玩耍交換顏色值後改變,但我似乎找到了一些我不很明白 - 我似乎不能找到一個很好的在谷歌上閱讀此事。我可以完成交換圖像的顏色,但與輸入文件的大小相比,它也會改變輸出文件的大小。文件大小是位圖
下面是一個測試類,我寫來測試事情,做什麼的,總結起來就是:
- 分配位圖到內存中。
- 使RGB值的陣列。
- 拆分陣列RGB值的成三個單獨的陣列(R,G和B)。
- 交換紅色所有的值(R [0] < - > R [1],R [2] < - >ř[3]等)
- 加入三個陣列並將其分配給RGB值的陣列。
- 複製回位圖。
- 釋放分配的內存。
- 導出文件。
守則,如下圖所示:
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;
}
}
}
我只是不明白爲什麼會改變位圖的圖像大小,因爲我沒有更換任何顏色值,僅僅是重新安排他們。
輸入文件的大小:[884 KB]
輸出文件的大小:[1335 KB]
無圖像不包含alpha通道:
Image.IsAlphaPixelFormat(image.PixelFormat) == false
什麼格式是輸入和輸出文件? – Reti43
輸入和輸出都是.png格式。我將位圖加載爲:'Bitmap bmp = new Bitmap(「../../ input.png」);'並將其導出爲'bmp.Save(「../../ output.png」) ;'@ Reti43 –
如果您加載輸入文件並立即保存而不做修改,它會與原始文件大小相同嗎? – Reti43