如何在VB.NET/C#.NET中将字节转换为颜色? [英] How do I convert a byte to a colour in VB.NET/C#.NET?

查看:112
本文介绍了如何在VB.NET/C#.NET中将字节转换为颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我想知道如何将字节转换为颜色。我的目标是从文件中创建一个位图,每个像素都有颜色。当转换该位图时,它将重新创建完整的文件。这是可能的。

问候,

iProgramIt

解决方案

你没有将byte转换成一个颜色。



您可以做的是获取您在.NET中可以访问的图像/位图,并以某种形式保存...一个字节数组,某事物或其他的序列化流等。



如果您愿意,您可以对图像进行下采样/转换;即,例如,如果从每像素24位彩色图像转换为每像素8位灰度图像。



你需要解释你的文件格式现在正在使用,你的目标是什么。


请看我对这个问题的评论。目前,.NET不支持这种像素格式。请特别参阅 PixelFormat Enumeration(System。 Drawing.Imaging) [ ^ ]。



但您可以通过映射转换为可用的像素格式之一来支持它您的颜色空间是.NET中使用的RGB空间之一,但您需要知道像素如何映射到颜色组件。



但是,您可能会告诉它我们不是你真正想要说的:每个颜色(和可选的alpha)组件每个像素1个字节,每个像素24或32位。那么这将是一个容易的问题。要解决此问题,您可以使用 System.Drawing 。以下是如何创建位图并用所需格式的像素填充它:

位图构造函数(Int32,Int32,PixelFormat)(System.Drawing) [ ^ ],

Bitmap.LockBits方法(System.Drawing) [ ^ ]。



代码示例可以在 LockBits 上的某个MSDN页面上找到:

Bitmap.LockBits方法(Rectangle,ImageLockMode,PixelFormat)(系统。绘图) [ ^ ]。



-SA

Hello all.
I was wondering how to convert a byte to a colour. What I am aiming to do is create a bitmap from a file which has colours in each pixel. When that bitmap is converted back, it would recreate the intact file. Is this possible.
Regards,
iProgramIt

解决方案

You don't convert a "byte" to a "color."

What you can do is take an image/bitmap you have access to in .NET, and save it in some form ... a byte array, a serialized stream of something-or-other, etc.

You can, if you wish, down-sample/transform the image; i.e., for example, convert if from 24 bits per pixel color image to 8 bit per pixel grey-scale image.

You need to explain the format of the file you are using now, and what your goal is.


Please see my comment to the question. Presently, .NET don't support such pixel formats. See, in particular, PixelFormat Enumeration (System.Drawing.Imaging)[^].

But you can support it by converting to one of the usable pixel formats, by mapping your color space to one the RGB spaces used in .NET, but you need to know how pixels are mapped to the color components.

However, it's possible that you told us not what you really meant to say: 1 byte per pixel per color (and optionally alpha) component, 24 or 32 bits per pixel. Then it would be an easy problem. To solve the problem, you can use System.Drawing. Here is how you can create a bitmap and fill it with pixels in required format:
Bitmap Constructor (Int32, Int32, PixelFormat) (System.Drawing)[^],
Bitmap.LockBits Method (System.Drawing)[^].

The code sample can be found on one of the MSDN pages on LockBits:
Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat) (System.Drawing)[^].

—SA


这篇关于如何在VB.NET/C#.NET中将字节转换为颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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