简体   繁体   English

在视图中显示来自多层.tiff的Image对象

[英]Display Image object, from multi-layer .tiff, in view

So, I am taking .tiff files from my local network, many have multiple layers. 因此,我从本地网络中获取.tiff文件,其中许多文件具有多层。 I convert each layer to a new Image object, which seems to work but now I am having trouble displaying each Image in my view. 我将每个图层转换为一个新的Image对象,该对象似乎可以工作,但是现在我在显示视图中的每个Image时遇到了麻烦。 I do not want to save these new Images in a format that could be displayed. 我不想以可能显示的格式保存这些新图像。 I am looking for a way to either draw the object or simply be able to display the picture or thumbnail. 我正在寻找一种方法来绘制对象或只是能够显示图片或缩略图。 Any suggestions would be appreciated. 任何建议,将不胜感激。

@foreach (var img in ViewBag.ImageData)
{
    <h2>@img</h2>
}

This does not display correctly. 这无法正确显示。

Controller: 控制器:

//Convert .tiff layers to Image and save in list
List<Image> images = new List<Image>();
Bitmap bitmap = (Bitmap)Image.FromFile("\path\190625141847.tif");
int count = bitmap.GetFrameCount(FrameDimension.Page);
for (int idx = 0; idx < count; idx++)
{
    // save each frame to a bytestream
    bitmap.SelectActiveFrame(FrameDimension.Page, idx);
    MemoryStream byteStream = new MemoryStream();
    bitmap.Save(byteStream, ImageFormat.Tiff);
    // and then create a new Image from it
    images.Add(Image.FromStream(byteStream));
}
// Returned to view in ViewBag
ViewBag.ImageData = images;

I may put the images into a carousel or the like, but I really am just trying to be able to display each Image (each layer) in my view. 我可以将图像放入轮播或类似的工具中,但实际上我只是想能够在视图中显示每个图像(每个图层)。

Well after some research and trial and error, I believe I figured it out for me at least. 经过一番研究和反复试验之后,我相信至少我能解决这个问题。 I changed up some of the code and instead of returning a List of System.Drawing.Image, I return a list of strings that correspond to the image URL. 我修改了一些代码,而不是返回System.Drawing.Image的列表,而是返回了与图像URL对应的字符串列表。

Here's my new controller code: 这是我的新控制器代码:

List<string> images = new List<string>();
Bitmap bitmap = (Bitmap)Image.FromFile("\\path\\190625141847.tif");
int count = bitmap.GetFrameCount(FrameDimension.Page);
for (int idx = 0; idx < count; idx++)
{
    // save each frame to a bytestream
    bitmap.SelectActiveFrame(FrameDimension.Page, idx);
    MemoryStream byteStream = new MemoryStream();
    bitmap.Save(byteStream, ImageFormat.Jpeg);

    // and then create a new URL from it
    byte[] bit = byteStream.ToArray();
    string imreBase64Data = Convert.ToBase64String(bit);
    string imgDataURL = string.Format("data:image/tif;base64,{0}", imreBase64Data);
    images.Add(imgDataURL);
    byteStream.Dispose();
}

return PartialView("_ImagesPartial", images);

I also returned a partial view but that's neither here or there. 我还返回了部分视图,但这不在这里或那里。 It's just for my project. 这只是我的项目。 Hope this helps someone. 希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM