0

I need to create a 16-bit grayscale image and for each pixel I want to set a different gray level in a range from 0 to 65535 (2 ^ 16 values​​).

I've tried using:

int height = 1000;
int width = 1280;
Bitmap image16bit = new Bitmap(width, height, PixelFormat.Format16bppGrayScale);

for (int r = 0; r < height; r++)
{
    for (int c = 0; c < width; c++)
    {
        Color nc = Color.FromArgb(255, 10, 20, 30);
        image16bit.SetPixel(c, r, nc);
    }
}

but does not work.

user3607507
  • 91
  • 2
  • 2
  • 8
  • 1
    define "does not work" – Tarec May 08 '14 at 09:40
  • 3
    You're creating an image that is 1280x1000 and therefore contains 1280000 pixels. You require each pixel to be a different level and yet you know that there are only 65536 possible values. And finally, I see no attempt to actually produce different levels. – Damien_The_Unbeliever May 08 '14 at 09:41
  • 2
    Actually I find this question interesting. The `SetPixel` method allows you to pass a `Color` object, which holds value on 32bits, but since `PixelFormat` is set to 16bits, it throws an `Invalid argument` exception. The amount of pixels and the way he's generating the colour is irrelevant here. – Tarec May 08 '14 at 09:48
  • It's not *irrelevant*. To avoid people going off on a tangent about what you *are* doing, as opposed to what you're asking about, make sure to include all the relevant details in the question. **Like for instance what "does not work" actually means.** Also, the code as written cannot possibly work even without the exception, since `Color`, though 32-bit, holds 8 bits per channel. – Lasse V. Karlsen May 08 '14 at 09:54
  • 1
    possible duplicate of [Set individual pixels in .NET Format16bppGrayScale image](http://stackoverflow.com/questions/3474434/set-individual-pixels-in-net-format16bppgrayscale-image) – Lasse V. Karlsen May 08 '14 at 09:56
  • @LasseV.Karlsen I agree, but "doesn't work" is not the same as "returns weird/incomplete results". It doesn't compile at all, so if the question lacks important details, no one should actually upvote the answers. – Tarec May 08 '14 at 09:56
  • You mean that it doesn't run. "Doesn't work" *can* be the same as "returns weird/incomplete results". It doesn't necessarily means that it throws an exception. – Lasse V. Karlsen May 08 '14 at 09:58
  • I read a post like this, but I can not find a correct for possible solutions. Any idea? http://social.msdn.microsoft.com/Forums/vstudio/en-US/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab/writing-a-16bit-grayscale-image?forum=csharpgeneral – user3607507 May 08 '14 at 11:55
  • @Damien_The_Unbeliever I want to set the value of each pixel, but need not be all different. – user3607507 May 08 '14 at 11:58

0 Answers0