gpt4 book ai didi

c# - 像素数据到图像

转载 作者:太空狗 更新时间:2023-10-29 20:56:45 25 4
gpt4 key购买 nike

我有这种格式的列表像素

ArrayList list = new ArrayList();
list.add(Red);
list.add(Blue);
list.add(Green);

我有很多这样的值(value)观。我想将数组列表转换为图像。尝试了很多但没有找到合适的 Material 。

编辑:这可能有助于:

List<byte> pic = new List<byte>(); 
for (int j = 0; j < headdata.Count; j++)
{
if(headdata.ElementAt(j).getHead() == i)
{
pic.Add((byte)headdata.ElementAt(j).getRed());
pic.Add((byte)headdata.ElementAt(j).getGreen());
pic.Add((byte)headdata.ElementAt(j).getBlue());
}
}

最佳答案

更新

我想我会改进这个答案以供将来引用。

将像素存储为无符号 32 位整数数组(C# uint)会更方便(并且性能更高)。这允许一次性设置每个像素的 4 个 channel (alpha、红色、绿色和蓝色)。如果您对使用 alpha channel (为了透明)不感兴趣,请参阅下面代码片段中有关像素格式的注释。

您现在表示颜色的方式是 32 位数字,例如 0xff0000,您可能会认为它类似于 CSS 十六进制颜色值。

// Image dimensions.
var width = 640;
var height = 480;

// Array to contain pixel data, with each element representing one pixel.
var pixelBuffer = new uint[width * height];

// When creating the bitmap, the operating system needs to be told the memory
// address of the pixel data array created above. This memory address can be
// found using a pinned GC handle, which also tells the GC (garbage collector)
// that it cannot move the array in memory after this point (as that would
// change the memory address).
//
// Note that the handle prevents the GC from freeing the object automatically,
// so remember to call the handle's `Free` method once finished (see below).
var pixelBufferHandle = GCHandle.Alloc(pixelBuffer, GCHandleType.Pinned);

// Create the bitmap using a constructor that takes a pointer to the array of
// pixel data:
//
// The `width` and `height` parameters are the image dimensions in pixels.
// These should match the values used above.
//
// The `stride` parameter specifies the number of _bytes_ required for each row
// of image data. Calculate this by multiplying the image's width by the number
// of bytes per pixel (here that's `sizeof(uint)`).
//
// The `format` parameter specifies how the colour data of each pixel is stored
// in the pixel data array:
// - For 32-bit RGB colour, use `Format32bppPArgb`. This signifies that the
// alpha channel is present (i.e. each pixel is 4 bytes) but should be
// ignored. Each pixel will be 100% opaque, and the alpha can be zero.
// - For 32-bit ARGB colour, use `Format32bppArgb`. Each pixel will be drawn
// opaque, semi-transparent, or fully transparent based on the alpha.
//
// The `scan0` parameter takes a pointer to the pixel data - use the GC handle
// created above to acquire the address of the array object.
var bitmap = new Bitmap(
width: width,
height: height,
stride: width * sizeof(uint),
format: PixelFormat.Format32bppPArgb,
scan0: pixelBufferHandle.AddrOfPinnedObject()
);

// At this point, the pixel data array can be freely manipulated independently
// of the bitmap. Each time the bitmap is draw, it will reflect the current
// content of the pixel data array.

// Once finished...

// Free the GC handle, allowing the GC to free the object automatically.
gchPixels.Free();

您可以创建在特定坐标处读取和写入像素的函数:

uint GetPixel(int x, int y) => pixels[x + y * width];
void SetPixel(int x, int y, uint argb) => pixels[x + y * width] = argb;

作为starbeamrainbowlabs指出:

GCHandleBitmapPixelFormat 位于 System.Runtime.InteropServicesSystem .DrawingSystem.Drawing.Imaging 分别。

下面是原始答案......

有一个很好的方法可以做到这一点,但它要求您使用数组或字节而不是列表。

考虑一下:

// Define the width and the height of the image
int width = 100;
int 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 the 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());

如果你想将其更改为 ARGB,只需将“3”的所有实例更改为“4”,并使用 PixelFormat.Format32bppArgb。

正如我所说,这要求您使用数组而不是列表,但是这只要你知道图像的宽度和高度,应该不会太难。在数组中设置单个像素的一个好方法是(也许制作一个方法为此):

pixels[x + y * width]     = R;
pixels[x + y * width + 1] = G;
pixels[x + y * width + 2] = B;

这个方法通常也很快做。

关于c# - 像素数据到图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16130248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com