【发布时间】:2019-11-29 11:21:27
【问题描述】:
利用this code 的后记,我能够编写一个函数来使用 Windows.Graphics.Imaging 解码多页 TIFF 文件:
private async Task TIFHandler( StorageFile file)
{
var random = new Random();
StorageFolder storage = null;
try
{
uint frameCount;
using (IRandomAccessStream randomAccessStream = await file.OpenAsync(FileAccessMode.Read, StorageOpenOptions.None))
{
Windows.Graphics.Imaging.BitmapDecoder bitmapDecoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(Windows.Graphics.Imaging.BitmapDecoder.TiffDecoderId, randomAccessStream);
frameCount = bitmapDecoder.FrameCount;
if (frameCount == 16)
{
StorageFolder mainfolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync((string)ApplicationData.Current.LocalSettings.Values["DynamicFolder"], CreationCollisionOption.OpenIfExists);
storage = await mainfolder.CreateFolderAsync(String.Format("{0:X6}", random.Next(0x1000000)), CreationCollisionOption.ReplaceExisting);
}
if (storage != null)
{
for (int frame = 0; frame < frameCount; frame++)
{
var bitmapFrame = await bitmapDecoder.GetFrameAsync(Convert.ToUInt32(frame));
var softImage = await bitmapFrame.GetSoftwareBitmapAsync();
byte[] array = null;
using (var ms = new InMemoryRandomAccessStream())
{
Windows.Graphics.Imaging.BitmapEncoder bitmapEncoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, ms);
bitmapEncoder.SetSoftwareBitmap(softImage);
try
{
await bitmapEncoder.FlushAsync();
}
catch (Exception ex) { }
array = new byte[ms.Size];
WriteableBitmap wb = new WriteableBitmap((int)bitmapDecoder.PixelWidth, (int)bitmapDecoder.PixelHeight);
using (Stream stream = wb.PixelBuffer.AsStream())
{
await stream.WriteAsync(array, 0, array.Length);
}
Guid BitmapEncoderGuid = Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId;
var bmif = await storage.CreateFileAsync($"X{frame}.png", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream = await bmif.OpenAsync(FileAccessMode.ReadWrite))
{
Windows.Graphics.Imaging.BitmapEncoder encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
Stream pixelStream = wb.PixelBuffer.AsStream();
byte[] pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
(uint)wb.PixelWidth,
(uint)wb.PixelHeight,
96.0,
96.0,
pixels);
await encoder.FlushAsync();
}
}
}
}
}
}
catch (Exception)
{
this.DynamicOperation.Text = "Error Reading TIFF Container";
ProgressBar.Visibility = Visibility.Collapsed;
AddTIFButton.IsEnabled = true;
if (storage != null) { await storage.DeleteAsync(); }
}
}
所以从外观上看,我似乎能够解码 TIFF 图像(我得到了正确的帧数),但是由于我收到了 BitmapFrame,所以我使用该函数将其转换为 SoftImage。 这次我使用 BitmapEncoder 将 SoftImage 保存为 PNG。这是我很难使用正确的方法将 SoftImage 保存为 PNG 文件的地方!
【问题讨论】: