0

Here is my code to construct an HDR image

                Bitmap hdrImage = new Bitmap(exposures[0].Width, exposures[0].Height, PixelFormat.Format16bppGrayScale);
                var rec = new Rectangle(0, 0, exposures[0].Width, exposures[0].Height);
                BitmapData bmData = hdrImage.LockBits(rec,
                   ImageLockMode.ReadWrite, hdrImage.PixelFormat);
                IntPtr intPtr = bmData.Scan0;
                //int size = bmData.Stride * bmData.Height;
                int size = bmData.Stride * bmData.Height;
                byte[] pixelValues = new byte[size];
                for (int i = numberOfExposures - 1; i >= 0; i--)
                {
                    var data = images[i].Buffer;
                    double A, B;
                    double factor = System.Math.Pow(f, numberOfExposures - 1 - i);
                    for (int p = 0; p < (images[0].Width * images[0].Height); p++)
                    {
                        if (i == numberOfExposures - 1)
                            A = 0.0;
                        else if (pixelValues[p] < 225 * factor / f)
                            A = 1.0;
                        else if (pixelValues[p] < 245 * factor / f)
                            A = 1.0 - ((pixelValues[p]) - 225.0 * factor / f) / ((245 - 225) * factor / f);         else
                            A = 0.0;
                        B = 1 - A;
                        double value = A * pixelValues[p] + B * (double)(data[p]) * factor;
                        pixelValues[p] = (byte)value;
                    }
                }
                System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, intPtr, size);
                hdrImage.UnlockBits(bmData);
                hdrImage.Save("TestImage.bmp");

when the code hits the hdrImage.Save("TestImage.bmp);, it throws "A Generic error occurred in GDI+" error.

I googled a little bit and all the answer seems to relate to the bitmap object is locked, but I've already unlock bits before saving it... Any ideas?

Thanks!

Liangze Lu
  • 31
  • 6
  • 1
    I think `Format16bppGrayScale` is not supported, see https://social.msdn.microsoft.com/Forums/en-US/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab/writing-a-16bit-grayscale-image?forum=csharpgeneral Also if it is actually 16 bits per pixel then `size` should be multiplied by `2` – Charlieface Mar 16 '22 at 20:59

0 Answers0