C# 用C语言将像素数组转换为图像#
我的C#程序中有一个C# 用C语言将像素数组转换为图像#,c#,java,arrays,pixels,C#,Java,Arrays,Pixels,我的C#程序中有一个int像素数组,我想把它转换成图像。问题是我正在将程序的Java源代码转换为等效的C代码。在java中,该行读取将int像素数组显示到图像中的内容: Image output = createImage(new MemoryImageSource(width, height, orig, 0, width)); 有人能告诉我C的等价物吗? 这里的orig是int像素的数组。我搜索了Bitmap类,有一个方法叫做SetPixel,但问题是它需要一个x,y坐标数。但是我的代码中
int
像素数组,我想把它转换成图像。问题是我正在将程序的Java源代码转换为等效的C代码。在java中,该行读取将int像素数组显示到图像中的内容:
Image output = createImage(new MemoryImageSource(width, height, orig, 0, width));
有人能告诉我C的等价物吗?
这里的orig是int
像素的数组。我搜索了Bitmap类,有一个方法叫做SetPixel
,但问题是它需要一个x,y坐标数。但是我的代码中有一个int
像素数组。另一件奇怪的事情是我的原始数组有负数,它们离255很远。在Java中,情况也是如此(这意味着C#和Java中的数组都有相同的值),并且这些值在Java中运行良好
但是我不能把这句话翻译成C。请提供帮助。您可以使用获取位图数据,然后可以直接操作,而不是通过SetPixel。()我假设每个int都是复合ARGB值?如果没有一个简单的选项,那么可能值得一看——它比
SetPixel
快得多,但更复杂。您还必须确保知道int是如何组成的(ARGB?RGBA?)。我将尝试看看是否有更明显的选项…使用WPF,您可以直接从阵列创建位图(图像)。然后,您可以对该图像进行编码、显示或播放:
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);
我建议使用锁位,但较慢的基于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;
}
//宽度-每行有多少整数
//数组-整数数组
位图createImage(int宽度,int[]数组)
{
整数高度=数组。长度/宽度;
位图bmp=新位图(宽度、高度);
对于(int y=0;y
的构造函数的第三个参数是一个整数数组,按该顺序由argb值组成
该页面中的示例通过创建这样一个数组
pix[index++] = (255 << 24) | (red << 16) | blue;
pix[index++]=(255我喜欢已经提供的WPF选项,但这里它使用的是锁位和位图
:
// 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);
//获取原始图像数据
int宽度、高度;
int[]data=GetData(向外宽度,向外高度);
//创建位图并对其进行操作
位图bmp=新位图(宽度、高度、像素格式.Format32bppArgb);
BitmapData bits=bmp.LockBits(新矩形(0,0,宽度,高度),
ImageLockMode.ReadWrite,bmp.PixelFormat);
不安全的
{
对于(int y=0;y
使用(作为测试数据):
publicstaticint[]GetData(out-int-width,out-int-height)
{
//矩形上的对角线渐变
宽度=127;
高度=128;
int[]数据=新int[宽度*高度];
对于(int x=0;x 数据[y*宽度+x]=0xFF+1。如果需要处理整个图片,则SetPixel/GetPixel的速度会很慢。负值可能是因为int值应该是无符号的。如果它们是有符号的,则不会有任何问题,但无符号会更有意义。我一直在寻找这个值,但我一直在玩弄rawstride值。您的代码工作得非常出色对我来说..谢谢…+1:)在(width*pf.BitsPerPixel+7)/8
中,7和8是从哪里来的?您的链接似乎已失效。
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;
}