【问题标题】:Display Image object, from multi-layer .tiff, in view在视图中显示来自多层 .tiff 的图像对象
【发布时间】:2019-09-06 21:54:17
【问题描述】:

所以,我从本地网络获取 .tiff 文件,其中许多文件具有多个层。我将每一层转换为一个新的图像对象,这似乎可以工作,但现在我无法在我的视图中显示每个图像。我不想以可以显示的格式保存这些新图像。我正在寻找一种方法来绘制对象或仅仅能够显示图片或缩略图。 任何建议,将不胜感激。

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

这显示不正确。

控制器:

//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;

我可能会将图像放入轮播等中,但我真的只是想在我的视图中显示每个图像(每一层)。

【问题讨论】:

  • “这显示不正确。” - 那它有什么作用呢?
  • 它只是显示一个默认缩略图......就像您尝试显示 .tiff 图像本身一样。

标签: c# image model-view-controller tiff


【解决方案1】:

经过一些研究和反复试验,我相信我至少自己想通了。我更改了一些代码,而不是返回 System.Drawing.Image 列表,而是返回与图像 URL 对应的字符串列表。

这是我的新控制器代码:

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);

我还返回了一个局部视图,但这既不是这里也不是那里。这只是为了我的项目。希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多