My code reads CZI file and extracts image from it. CZI file has multiple time points and three channels (blue, red and grayscale). Reading blue channel always produces 1px bmp for certain files with a single scene (I have other files with multiple scenes, and blue channel in those files is read correctly).
I create instance of ImageAccessor which reports non-zero byte size. But when I read it using method ImageAccessor.GetBitmapSource() I get back 1 pixel size image. This happens for blue channel only, other channels produce correct images when read using the same method.
If I open the same file with ZEN(blue) I see three channels. I'm out of ideas what I could be doing wrong.
Here is a code snippet:
Code: Select all
ImageDocument imageDocument = new ImageDocument();
imageDocument.Open("c:\file.czi");
string pixelType = imageDocument.PixelType.ToString();
int sizeS = imageDocument.Bounds.SizeS;
int sizeC = imageDocument.Bounds.SizeC;
int sizeT = imageDocument.Bounds.SizeT;
int sizeB = imageDocument.Bounds.SizeB;
int sizeZ = imageDocument.Bounds.SizeZ;
int width = imageDocument.Bounds.SizeX;
int height = imageDocument.Bounds.SizeY;
// split by channel - array count depends on number of time points and scenes and is a multiplication of these counts
ImageAccessorCollection c = imageDocument.CreateAccessor().Split3d(ImageDimension.C);
for (int i = 0; i < c.Count; i++)
{
// split by time - array count is equal to channel count
ImageAccessorCollection t = c[i].Split3d(ImageDimension.T);
for (int j = 0; j < t.Count; j++)
{
// frame position on the image surface
int channel = t[j].Bounds.StartC;
int time = t[j].Bounds.StartT;
int slice = t[j].Bounds.StartZ;
int scene = t[j].Bounds.StartS;
int block = t[j].Bounds.StartB;
int ntime = t[j].Bounds.SizeT;
int nscene = t[j].Bounds.SizeS;
int nblock = t[j].Bounds.SizeB;
// pixel type
string pixel = t[j].PixelType.ToString();
// frame dimensions
int x = t[j].FirstPixelSubBlock.Bounds.StartX;
int y = t[j].FirstPixelSubBlock.Bounds.StartY;
int z = t[j].FirstPixelSubBlock.Bounds.StartZ;
int w = t[j].FirstPixelSubBlock.Bounds.SizeX;
int h = t[j].FirstPixelSubBlock.Bounds.SizeY;
int d = t[j].FirstPixelSubBlock.Bounds.SizeZ;
string dir = "c:\output";
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
string filePath = Path.Combine(dir, string.Format("t{:D 6}_c{1 :D 2}_s{2 :D 2}_z{3 :D 2}.bmp", time, channel, scene, slice));
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
ImageAccessor imageAccessor = t[j].CreateAccessor(new SubsetBounds { SizeX = w, SizeY = h, SizeZ = d, StartX = x, StartY = y,
StartZ = z, SizeS = nscene, StartS = scene, SizeT = ntime, StartT = time });
var image = imageAccessor.GetBitmapSource();
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(fileStream);
}
}
}