需要帮助使用内存流作为 TiffBitmap 解码器的输入
Need assistance with using memory stream as input for TiffBitmap decoder
我正在用 C# 和 WPF 为存储在 SQL 服务器数据库 image 列中的 TIFF 图像编写图像查看器。我已经使用 GetBytes 循环将图像的检索编码到内存流中,并且可以正常工作。不起作用的是从内存流创建 TiffBitmapDecoder 并将其用作 WPF/XAML Image 控件的 BitmapSource。这是我的 return 以内存流作为输入的 BitmapSource 的函数:
namespace ViewDBImages
{
public static class Utility
{
public static BitmapSource StreamToImage(MemoryStream imageMem)
{
//
// Decode the Memory Stream argument into a Bitmap with TIFF format
// First we have to set the Stream seek location to the origin
//
imageMem.Seek(0, SeekOrigin.Begin);
//
// Decode the stream into a TiffBitmap and return it as the image source
//
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageMem, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand);
BitmapSource source = decoder.Frames[0];
return source;
}
}
}
我相信图像被设置为图像控件的源,因为我可以看到滚动条在检索时发生变化,但它们不可见。我还看到此函数完成后内存流的位置为“8”,但流的大小为 63,083 字节。
为了帮助调试,我将内存流复制到 TIFF 文件并将其用作解码器的流输入。图像以这种方式正确显示。所以我怀疑当图像存储为文件时一定有某种控制信息可用,但在内存流中找不到。这是代码:
namespace ViewDBImages
{
public static class Utility
{
public static BitmapSource StreamToImage(MemoryStream imageMem)
{
//
// Copy the Memory Stream argument into a filestream and save as a TIF file
// First we have to set the Stream seek location to the origin
//
imageMem.Seek(0, SeekOrigin.Begin);
FileStream imageFile = new FileStream(@"C:\Image Test\Testfile.tif", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
imageMem.CopyTo(imageFile);
//
// Decode the file to a TiffBitmap and return it as the image source
//
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageFile, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand);
BitmapSource source = decoder.Frames[0];
return source;
}
}
}
感谢您的任何建议。
尝试设置BitmapCacheOption.OnLoad
使解码器立即加载位图:
var decoder = new TiffBitmapDecoder(
imageMem, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
return source = decoder.Frames[0];
请注意,您也可以直接从流创建 BitmapFrame。自动选择合适的解码器。
return BitmapFrame.Create(
imageMem, BitmapCreateOptions.None, BitmapCacheOption.OnLoad)
我正在用 C# 和 WPF 为存储在 SQL 服务器数据库 image 列中的 TIFF 图像编写图像查看器。我已经使用 GetBytes 循环将图像的检索编码到内存流中,并且可以正常工作。不起作用的是从内存流创建 TiffBitmapDecoder 并将其用作 WPF/XAML Image 控件的 BitmapSource。这是我的 return 以内存流作为输入的 BitmapSource 的函数:
namespace ViewDBImages
{
public static class Utility
{
public static BitmapSource StreamToImage(MemoryStream imageMem)
{
//
// Decode the Memory Stream argument into a Bitmap with TIFF format
// First we have to set the Stream seek location to the origin
//
imageMem.Seek(0, SeekOrigin.Begin);
//
// Decode the stream into a TiffBitmap and return it as the image source
//
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageMem, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand);
BitmapSource source = decoder.Frames[0];
return source;
}
}
}
我相信图像被设置为图像控件的源,因为我可以看到滚动条在检索时发生变化,但它们不可见。我还看到此函数完成后内存流的位置为“8”,但流的大小为 63,083 字节。
为了帮助调试,我将内存流复制到 TIFF 文件并将其用作解码器的流输入。图像以这种方式正确显示。所以我怀疑当图像存储为文件时一定有某种控制信息可用,但在内存流中找不到。这是代码:
namespace ViewDBImages
{
public static class Utility
{
public static BitmapSource StreamToImage(MemoryStream imageMem)
{
//
// Copy the Memory Stream argument into a filestream and save as a TIF file
// First we have to set the Stream seek location to the origin
//
imageMem.Seek(0, SeekOrigin.Begin);
FileStream imageFile = new FileStream(@"C:\Image Test\Testfile.tif", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
imageMem.CopyTo(imageFile);
//
// Decode the file to a TiffBitmap and return it as the image source
//
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageFile, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand);
BitmapSource source = decoder.Frames[0];
return source;
}
}
}
感谢您的任何建议。
尝试设置BitmapCacheOption.OnLoad
使解码器立即加载位图:
var decoder = new TiffBitmapDecoder(
imageMem, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
return source = decoder.Frames[0];
请注意,您也可以直接从流创建 BitmapFrame。自动选择合适的解码器。
return BitmapFrame.Create(
imageMem, BitmapCreateOptions.None, BitmapCacheOption.OnLoad)