gpt4 book ai didi

c# - View 是否可以在不先将其保存到文件的情况下显示 WebImage?

转载 作者:太空狗 更新时间:2023-10-29 20:52:02 25 4
gpt4 key购买 nike

我不太确定我是否在使用 WebImage正确分类。

我有一个 Controller ,可以从数据库中提取照片和一些相关信息(评论、上传日期、文件名)。我想返回包含此信息的部分 View ,并显示图像和额外信息。

所以我从字节数组创建了一个新的 WebImage,但是我该如何显示它呢?

根据 this article应该很简单

  1. You need to work with Razor syntax and create a variable that will contain the image:
    @{ var myImage = WebImage("/Content/myImage.jpg") // Work with the image… }

  2. Then, in order to load the image in the page, you have to show the variable that contains the image inside an HTML <img/> tag:
    <img src="@myImage"/>

除了不起作用,它只输出<img src="System.Web.Helpers.WebImage">并调用 .Write没有帮助。

有什么方法可以做到这一点,或者我是否需要将我的操作分成两个不同的操作,一个返回照片信息,一个返回照片本身?

最佳答案

你可以即时写出来!

您只是不像您想象的那样使用 WebImage.Save(),而是使用 WebImage.GetBytes()。

在此示例中,您已经将图像作为字节数组保存到数据库中。将其简化为仅处理 jpeg。

    /// <summary>
/// Reference this in HTML as <img src="/Photo/WatermarkedImage/{ID}" />
/// Simplistic example supporting only jpeg images.
/// </summary>
/// <param name="ID">Photo ID</param>
public ActionResult WatermarkedImage(Guid ID)
{
// Attempt to fetch the photo record from the database using Entity Framework 4.2.
var photo = db.Photos.Find(ID);

if (photo != null) // Found the indicated photo record.
{
// Create WebImage from photo data.
// Should have 'using System.Web.Helpers' but just to make it clear...
var wi = new System.Web.Helpers.WebImage(photo.Data);

// Apply the watermark.
wi.AddImageWatermark(Server.MapPath("~/Content/Images/Watermark.png"),
opacity: 75,
horizontalAlign: "Center",
verticalAlign: "Bottom");

// Extract byte array.
var image = wi.GetBytes("image/jpeg");

// Return byte array as jpeg.
return File(image, "image/jpeg");
}
else // Did not find a record with passed ID.
{
return null; // 'Missing image' icon will display on browser.
}
}

关于c# - View 是否可以在不先将其保存到文件的情况下显示 WebImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6765638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com