在C#转换像素阵列到图像 [英] Converting an array of Pixels to an image in C#

查看:819
本文介绍了在C#转换像素阵列到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

INT 像素在我的C#程序中的数组,我想它转换成图像。问题是我转换成Java源代码code的程序转换成等价的C#code。 Java中的行读取它显示的整数像素数组转换为图像:

I have an array of int pixels in my C# program and I want to convert it into an image. The problem is I am converting Java source code for a program into equivalent C# code. In java the line reads which displays the array of int pixels into image:

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

有人可以告诉我C#相同呢?

下面是原稿 INT 像素阵列。我搜索了Bitmap类,有一个名为的setPixel 方法,但问题是它需要的x,y坐标数字。但我有我的code是 INT 像素阵列。另一个奇怪的是我的原稿数组有负数,他们是这样远离255在Java中,这是不变的情况下(这意味着无论是在C#和Java具有同等价值的阵列)和值在Java中工作正常。

Here orig is the array of int pixels. I searched the Bitmap class and there is a method called SetPixel but the problem is it takes a x,y coordinate number. But what I have in my code is an array of int pixels. Another weird thing is my orig array has negative number and they are way far away from 255. In Java this is the same case (meaning both the array in C# and Java have equivalent value) and the values is working fine in Java.

但我不能让该行翻译成C#。请帮助。

But I can't get that line translated into C#. Please help.

推荐答案

使用WPF,您可以创建直接从您的阵列位图(图片)。然后,您可以连接code本图片或显示,或用它玩:

Using WPF, you can create a bitmap (image) directly from your array. You can then encode this image or display it or play with it:

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);

这篇关于在C#转换像素阵列到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆