0

I am trying to create a BitMap from a string array of pixel color values. The array contains 76800 elements (320 x 240) which have the pixels in decimal format (e.g. "11452343").

I am trying to use this function to create my BitMap

var b = new Bitmap(320, 240, PixelFormat.Format8bppIndexed);
var ncp = b.Palette;
for (int i = 0; i < 256; i++)
    ncp.Entries[i] = Color.FromArgb(255, i, i, i);
b.Palette = ncp;

var BoundsRect = new Rectangle(0, 0, 320, 240);
var bmpData = b.LockBits(BoundsRect, ImageLockMode.WriteOnly, b.PixelFormat);

var ptr = bmpData.Scan0;
var bytes = bmpData.Stride * b.Height;
var rgbValues = new byte[bytes];

for (var i = 0; i < pixelArray.Length; i++)
{
    // ISSUE OCCURS HERE
    rgbValues[i] = byte.Parse(pixelArray[i]);
}

Marshal.Copy(rgbValues, 0, ptr, bytes);
b.UnlockBits(bmpData);

My issue occurs inside the for loop where I try to convert the decimal value to a byte value to add to the rgbValues array. How can I convert that decimal color to a byte value to add to the array?

Frank Johnson
  • 163
  • 1
  • 12
  • 1
    what is the pixelformat of the string color and do you need the Bitmap to be in PixelFormat.Format8bppIndexed? – T_D Jun 13 '14 at 18:48
  • 1
    _the pixels in decimal format (e.g. "11452343")_ is that r*g*b? or just a typo? – TaW Jun 13 '14 at 18:53
  • A byte is -128 to 127 by definition so you can't; unless you use encoding. – Gabe Jun 13 '14 at 18:56
  • 1
    @Gabe: Actually, [byte](http://msdn.microsoft.com/en-us/library/5bdb6693.aspx) is 0 to 255. [sbyte](http://msdn.microsoft.com/en-us/library/d86he86x.aspx) is -128 to 127. – Jim Mischel Jun 13 '14 at 18:58
  • Ah right, in C# bytes are implicitly unsigned – Gabe Jun 13 '14 at 19:00
  • What is `pixelArray`? Is it an array of bytes? Integers? Strings? – Jim Mischel Jun 13 '14 at 19:02
  • The array contains string values of the pixel. The pixel is ARGB – Frank Johnson Jun 13 '14 at 19:03
  • Do you know how many unique colors are in the input? Since you're using 8bpp Indexed format, your ARGB colors need to be mapped to an entry in the color palette `ncp` somehow. – pmcoltrane Jun 13 '14 at 19:07
  • You need a dictionary or some other lookup mechanism that will convert the ARGB value to the associated pixel index value. If the ARGB values are not the same as your 256 color table values, then you need to do some kind of conversion. Here, it looks like you're creating a grayscale bitmap. If that's the case, you should see http://stackoverflow.com/questions/2265910/convert-an-image-to-grayscale, and the other questions related to that. – Jim Mischel Jun 13 '14 at 19:11
  • _The pixel is ARGB_ so _"11452343"_ was a typo? Frank, we can help you better, if you are precise in your question.. – TaW Jun 13 '14 at 19:56

1 Answers1

1

From what I hope I understand I think you are not doing this right..

I see you are building a greyscale palette but the ARGB values will not point into that palette..

Assuming that you have a decimal number for each pixel, I think it would be easier to first create a Bitmap with default ARGB color depth.

After you have painted its pixels you could change its format to Format8bppIndexed.

Creating the palette and the pixel index values will be best done by system routines.

Edit: If you really want to create the mapping yourself, you can use this line to create a greyscale 8-bit value from a decimal ARGB value decVal:

int grey = (byte)(Color.FromArgb(decVal).GetBrightness() * 255);

Setting the pixels to these byte values should create a working mapping into your greyscale palette but it I haven't tried it myself yet..

TaW
  • 53,122
  • 8
  • 69
  • 111