更新
以为我会改进这个帖子以供将来参考。
如果将像素数据存储为32位整数(理想情况下无符号数组),则更容易。每个整数的4个字节应分别对应于alpha,红色,绿色和蓝色通道。如果您不使用Alpha通道,请将其保持为空。
public void SetPixel(int x, int y, uint colour) => pixels[x + y * width] = colour;
由于starbeamrainbowlabs指出:
GCHandle
,Bitmap
,并PixelFormat
在System.Runtime.InteropServices
发现,System.Drawing
// If your image is always going to be the same size, make these constant -
// it speeds things up tremendously.
var width = 640;
var height = 480;
// Create the array to contain bitmap's pixel data.
// Making the pixels unsigned *can* speed up certain arithmetic operations,
// so use them if you're going to be manipulating colours as integers in the array.
// Note that array size is width * height only.
var pixels = new uint[width * height];
/* <Perform array operations here> */
// Create a handle to the object on the heap.
// Making it 'Pinned' prevents the GC from moving it around in memory.
var gchPixels = GCHandle.Alloc(pixels, GCHandleType.Pinned);
// Create the bitmap itself.
// Note: if you plan on making use of the alpha channel, make sure you use
// Format32bppArgb instead (no P).
// Format32bppPArgb means that the alpha channel is present but should be ignored.
var bitmap = new Bitmap(width, height, width * sizeof(uint),
PixelFormat.Format32bppPArgb,
gchPixels.AddrOfPinnedObject());
/* <Perform bitmap operations here> */
// Free the handle to stop the GC from pinning the array.
// This is important if you were dealing with a large array.
gchPixels.Free();
您可以在某个坐标设定像素创建你自己的方法,和System.Drawing.Imaging
。
原来的答案下面
有这样做的相当的好方法,但它需要你使用数组或字节,而不是一个列表。如果你想改变这是ARGB,只需更改的 '3' 所有实例 '4', 和使用PixelFormat.Format32bppArgb
// Define the width and the height of the image
int width = 100,
height = 100;
/* Create an array of bytes consisting of the area's worth of pixels
* with 3 bytes of data each.
*/
byte[] pixels = new byte[width * height * 3];
/* > Code for making the image etc. here < */
// Create our bitmap.
/* GCHandle requires System.Runtime.InteropServices
* PixelFormat requires System.Drawing.Imaging.
*/
Bitmap bmp = new Bitmap(width, height, width * 3, PixelFormat.Format24bppRgb,
GCHandle.Alloc(pixels, GCHandleType.Pinned).AddrOfPinnedObject());
:
考虑这一点。
正如我所说的,这需要你使用一个数组而不是一个列表,但这个 应该不会太困难,只要你知道图像的宽度和高度。 数组中设置一个单独的像素(也许会让一个方法 本)的一个好办法:
pixels[x + y * width] = R;
pixels[x + y * width + 1] = G;
pixels[x + y * width + 2] = B;
这种方法一般是相当快过,如果这是在所有必要的不管你是 做。
来源:经验。
为什么你使用ArrayList存储像素数据? – 2013-04-21 10:36:17
我正在聚集图像,所以我不知道从开始的图像集群的确切大小,如果我在大型数据集上运行一次循环,它需要很多时间..所以我使用数组列表进行动态分配 – greatmajestics 2013-04-21 10:37:13
什么是您的项目类型? 'WPF','Windows Form',... – 2013-04-21 10:39:46