24 or 32 bit image with Color List in WPF

Hi, I trying to create bitmap and use the code I found on internet.
  private void Button_Click(object sender, RoutedEventArgs e)
        {
            var width = 300;
            var height = 300;
            List<System.Windows.Media.Color> colors = new
List<System.Windows.Media.Color>();
            colors.Add(System.Windows.Media.Colors.Red);
            colors.Add(System.Windows.Media.Colors.White);
            colors.Add(System.Windows.Media.Colors.Blue);
            colors.Add(System.Windows.Media.Colors.Green);
            BitmapPalette myPalette = new BitmapPalette(colors);


            var bitmap = new WriteableBitmap(width, height, 96, 96,
PixelFormats.Pbgra32, myPalette);
            var stride = (bitmap.Width * bitmap.Format.BitsPerPixel +
7) / 8;
            var pixels = new ushort[width * height];
            for (var y = 0; y < height; ++y)
                for (var x = 0; x < width; ++x)
                {
                    var v = (0x10000 * 2 * x / width + 0x10000 * 3 * y
/ height);
                    var isMirror = (v / 0x10000) % 2 == 1;
                    v = v % 0xFFFF;
                    if (isMirror)
                        v = 0xFFFF - v;

                    pixels[y * width + x] = (ushort)v;
                }

            bitmap.WritePixels(new Int32Rect(0, 0, width, height),
pixels, stride, 0);

            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmap));
            using (var stream = System.IO.File.Create("my.png"))
           encoder.Save(stream);
            imageDisplay.Source = bitmap;

        }
I just changed PixelFormats from Gray16 to Pbgra32 and added the color list. then the code stops to works.
In initial version it was this line: bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 2, 0); in my version
            bitmap.WritePixels(new Int32Rect(0, 0, width, height),
pixels, stride, 0);
If I replace stride with width * 2 (or width * 32) the buffer is not enough . If I use stride then there is conflict between pixels which are declared as ushort.
How can I create 24 or 32 bit image with color list?
LVL 1
mastiSoftAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
When I compile your code, I see error in this row
 bitmap.WritePixels(new Int32Rect(0, 0, width, height),
pixels, stride, 0); 

Open in new window

The third parameter must be int.

Try to change one row:
var pixels = new ushort[(int)(stride * bitmap.Height)];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mastiSoftAuthor Commented:
Thank you very much Misha. It works now . But I had to change the last line also :bitmap.WritePixels(new Int32Rect(0, 0, width, height),pixels, (int)stride, 0);
Thank you for the quick answer!
0
MishaProgrammerCommented:
glad to help you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WPF

From novice to tech pro — start learning today.