Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 29-Jan-16 22:24pm    
It depends on representation of colors in a byte (so called "pixel format"). Such color formats with 8 bits for all color components are extremely rare today, the can be considered purely historical (not counting two-color indexed format which is still supported by .NET, by some weird reason). Presently widely used formats use 24 to 64 bits per pixel, most popular being 24 RGB and 32 ARGB per pixel (8 bits per color).
—SA

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.
 
Share this answer
 
Comments
iProgramIt 31-Jan-16 5:11am    
1. I have explained my goal.
2. I don't want a picture. I would like an image representation of a file. Say, an exe file or along those lines.
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
 
Share this answer
 
Comments
iProgramIt 31-Jan-16 5:12am    
Thank you. I am in the process of learning GDI and about bitmaps. So help is greatly appreciated.
Sergey Alexandrovich Kryukov 31-Jan-16 11:39am    
You are welcome. Will you accept the answer formally?
—SA
iProgramIt 31-Jan-16 16:48pm    
Yes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900