I need to save a intraoral x-ray image, I'm try to use the 8bppIndexed pixel format, and using this code to save:
private void SaveImage(short[] data, int widht, int height)
{
try
{
Bitmap pic = new Bitmap(widht, height, PixelFormat.Format8bppIndexed);
Rectangle dimension = new Rectangle(0, 0, pic.Width, pic.Height);
BitmapData picData = pic.LockBits(dimension, ImageLockMode.ReadWrite, pic.PixelFormat);
IntPtr pixelStartAddress = picData.Scan0;
Marshal.Copy(data, 0, pixelStartAddress, data.Length);
pic.UnlockBits(picData);
pic = ConvertToGrayscale(pic);
pic.Save("C:\\Users\\WIM\\Desktop\\teste\\teste\\teste.jpeg");
pic.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
And with this line Marshal.Copy(data, 0, pixelStartAddress, data.Length);
I receive the AcessViolationException.
I'm not used to c# so I really don't undertand why I got this error. What I read is that maybe the ptr has been in use, but if I use other pixelFormat, like 16bppRgb565, I can capture the image and transform to grayScale, but the image return deformed. Like this
I'm using an array of short because It's the data I received from the sdk I received from the company we bought the sensor;
In my researches, I found the 16bppGrayScale is a good option, but the GDI+ don't have it encoded yet. But we can make a DICOM for this, but we need to run the application on nootbooks with a hardware not that good, so for that I'm trying to use the 8bppIndexed pixel format.