【问题标题】:Convert image from sitecore field to base64 string将图像从 sitecore 字段转换为 base64 字符串
【发布时间】:2014-08-22 05:12:55
【问题描述】:

如果有人可以帮助我,我需要一些帮助来解决问题。我正在尝试从 Sitecore 字段中获取图像,然后将其转换为 base64 字符串。

我所做的是将该内容转换为 ImageField 数据类型,但我似乎找不到将其转换为 base64 的解决方案。

Sitecore.Data.Fields.ImageField img = itm.Fields["image"];

有人可以帮助我吗?

最好的问候, 伊诺特。

【问题讨论】:

    标签: image base64 sitecore converter sitecore6


    【解决方案1】:

    您需要从图像字段中获取链接的媒体项,并从媒体项文件流中创建 base64 字符串:

    ImageField imageField = itm.Fields["Image"];
    MediaItem mediaItem = imageField.MediaItem;
    Stream stream = mediaItem.GetMediaStream();
    Byte[] bytes = new Byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    img64.Src = "data:" + mediaItem.MimeType + ";base64," + Convert.ToBase64String(bytes);
    

    【讨论】:

    • 非常感谢。这对我来说就像一个魅力。
    • 你好。再次回到与此有关的另一个问题。有谁知道我如何从sitecore获取图像,但尺寸为440x80?我不想在 sitecore 中更改它,只是以特定大小阅读它。
    • 我对这个问题还有另一个问题。如何将通过的所有图像转换为 PNG 或 JPEG?我目前也得到 GIF 图片,这对我不利。
    猜你喜欢
    • 2018-09-07
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多