在C#中将像素数组转换为图像

我的C#程序中有一个int像素数组,我想将其转换为图像。 问题是我正在将程序的Java源代码转换为等效的C#代码。 在java中,行读取将int像素数组显示为image:

 Image output = createImage(new MemoryImageSource(width, height, orig, 0, width)); 

有人能告诉我C#等价吗?

这里的orig是int像素的数组。 我搜索了Bitmap类,有一个名为SetPixel的方法,但问题是需要ax,y坐标数。 但我在我的代码中有一个int像素数组。 另一个奇怪的事情是我的orig数组有负数,它们远远超过255.在Java中,这是相同的情况(意味着C#和Java中的数组都具有相同的值),并且这些值在Java中运行良好。

但我无法将该行转换为C#。 请帮忙。

使用WPF,您可以直接从arrays创建位图(图像)。 然后,您可以对此图像进行编码或显示或使用它:

 int width = 200; int height = 200; // // Here is the pixel format of your data, set it to the proper value for your data // PixelFormat pf = PixelFormats.Bgr32; int rawStride = (width * pf.BitsPerPixel + 7) / 8; // // Here is your raw data // int[] rawImage = new int[rawStride * height / 4]; // // Create the BitmapSource // BitmapSource bitmap = BitmapSource.Create( width, height, 96, 96, pf, null, rawImage, rawStride); 

您可以使用Bitmap.LockBits获取可以直接操作的位图数据,而不是通过SetPixel。 ( 如何使用LockBits )

我喜欢已经提供的WPF选项,但这里使用的是LockBitsBitmap

  // get the raw image data int width, height; int[] data = GetData(out width, out height); // create a bitmap and manipulate it Bitmap bmp = new Bitmap(width,height, PixelFormat.Format32bppArgb); BitmapData bits = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bmp.PixelFormat); unsafe { for (int y = 0; y < height; y++) { int* row = (int*)((byte*)bits.Scan0 + (y * bits.Stride)); for (int x = 0; x < width; x++) { row[x] = data[y * width + x]; } } } bmp.UnlockBits(bits); 

用(作为测试数据):

  public static int[] GetData(out int width, out int height) { // diagonal gradient over a rectangle width = 127; height = 128; int[] data = new int[width * height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int val = x + y; data[y * width + x] = 0xFF << 24 | (val << 16) | (val << 8) | val; } } return data; } 

好吧,我假设每个int都是复合ARGB值? 如果没有一个简单的选项,那么LockBits可能值得一试 – 它比SetPixel快得多,但更复杂。 您还必须确保知道int是如何组成的(ARGB?RGBA?)。 我会试着看看是否有更明显的选择……

MemoryImageSource的构造函数的第三个参数是一个由这个顺序的argb值组成的int数组

该页面中的示例通过以下方式创建此类数组;

 pix[index++] = (255 << 24) | (red << 16) | blue; 

你需要将整数数组分解为一个字节数组(移位运算符会很有用),但它应该是bgr顺序,以便LockBits方法工作。

我建议使用LockBits,但较慢的基于SetPixel的算法可能看起来像

 // width - how many int's per row // array - array of integers Bitmap createImage(int width, int[] array) { int height = array.Length / width; Bitmap bmp = new Bitmap(width, height); for (int y = 0; y < height; y++) { for (int x = 0; x < array.Length; x += width) { bmp.SetPixel(x, y, Color.FromArgb(array[i])); } } return bmp; }