Подозреваю, что проблема должна быть здесь:
Код: Выделить всё
int w = grabResult.Width, h = grabResult.Height;
try
{
var converter = new PixelDataConverter { OutputPixelFormat = PixelType.Mono16 };
byte[] buffer16LE = new byte[w * h * 2];
converter.Convert(buffer16LE, grabResult);
ushort[] mono12 = new ushort[w * h];
Buffer.BlockCopy(buffer16LE, 0, mono12, 0, buffer16LE.Length);
int maxVal = mono12.Take(Math.Min(1000, mono12.Length)).Max();
bool msbAligned = maxVal > 4095;
if (msbAligned)
{
for (int i = 0; i < mono12.Length; i++)
mono12[i] = (ushort)((mono12[i] >> 4) & 0x0FFF);
}
else
{
for (int i = 0; i < mono12.Length; i++)
mono12[i] &= 0x0FFF; // Ensure bits 12-15 are zero
}
byte[] outLE = new byte[mono12.Length * 2];
Buffer.BlockCopy(mono12, 0, outLE, 0, outLE.Length);
string outPath = (config?.SaveImageMode == "overwrite") ? test_image : tiffPath;
Directory.CreateDirectory(Path.GetDirectoryName(outPath) ?? ".");
SaveTiff16Gray(outLE, w, h, outPath);
}
catch
{
throw;
}
}
Код: Выделить всё
private static void SaveTiff16Gray(byte[] buffer16LE, int width, int height, string path)
{
try
{
var source = BitmapSource.Create(
width, height,
96, 96,
PixelFormats.Gray16,
null,
buffer16LE,
width * 2
);
var encoder = new TiffBitmapEncoder { Compression = TiffCompressOption.None };
encoder.Frames.Add(BitmapFrame.Create(source));
var dir = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(dir)) dir = ".";
Directory.CreateDirectory(dir);
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
encoder.Save(fs);
fs.Flush();
}
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to save TIFF: {ex.Message}");
throw;
}
}
Ожидание: получить такое же изображение, как если бы я сохранял его с помощью приложения Basler
Подробнее здесь: https://stackoverflow.com/questions/797 ... save-image