MVC 中的动态图像(Stack Exchange Beta 站点)

发布于 2024-09-09 04:11:23 字数 219 浏览 3 评论 0原文

我一直在查看 Stack Exchange Beta 网站,并注意到每个进入测试版的网站顶部都有一个图形,其中包含“Web 应用程序 Beta”或“游戏 Beta”等字样。

我想知道这些图像是单独创建的还是以某种方式动态创建的?有没有办法使用 MVC.NET 动态创建 PNG?

如果这个论坛的答案太复杂,有人可以给我指出一些有帮助的文章吗?

预先感谢

嗅探者

I've been looking at the Stack Exchange Beta sites and noticed that each site that goes into beta has a graphic at the top with the words 'Web Applications Beta' or 'Gaming Beta' for example.

I wondered if these images are individually created or if the are created dynamically somehow? Is there a way using MVC.NET to create PNGs on the fly?

If the answer is too complicated for this forum, can anybody point me to any articles that will help?

Thanks in advance

Sniffer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-09-16 04:11:23

是的,有一种方法可以使用 ASP.NET MVC 动态生成和提供图像。这是一个示例:

public class HomeController : Controller
{
    public ActionResult MyImage()
    {
        // Load an existing image
        using (var img = Image.FromFile(Server.MapPath("~/test.png")))
        using (var g = Graphics.FromImage(img))
        {
            // Use the Graphics object to modify it
            g.DrawLine(new Pen(Color.Red), new Point(0, 0), new Point(50, 50));
            g.DrawString("Hello World", 
                new Font(FontFamily.GenericSerif, 20), 
                new Pen(Color.Red, 2).Brush, 
                new PointF(10, 10)
            );

            // Write the resulting image to the response stream
            using (var stream = new MemoryStream())
            {
                img.Save(stream, ImageFormat.Png);
                return File(stream.ToArray(), "image/png");
            }
        }
    }
}

然后只需将此图像包含在视图中即可:

<img src="<%= Url.Action("myimage", "home") %>" alt="my image" />

Yes there is a way to generate and serve images dynamically with ASP.NET MVC. Here's an example:

public class HomeController : Controller
{
    public ActionResult MyImage()
    {
        // Load an existing image
        using (var img = Image.FromFile(Server.MapPath("~/test.png")))
        using (var g = Graphics.FromImage(img))
        {
            // Use the Graphics object to modify it
            g.DrawLine(new Pen(Color.Red), new Point(0, 0), new Point(50, 50));
            g.DrawString("Hello World", 
                new Font(FontFamily.GenericSerif, 20), 
                new Pen(Color.Red, 2).Brush, 
                new PointF(10, 10)
            );

            // Write the resulting image to the response stream
            using (var stream = new MemoryStream())
            {
                img.Save(stream, ImageFormat.Png);
                return File(stream.ToArray(), "image/png");
            }
        }
    }
}

And then simply include this image in the view:

<img src="<%= Url.Action("myimage", "home") %>" alt="my image" />
请远离我 2024-09-16 04:11:23

这对我有用。

using System.Web.Helpers;
public class HomeController : Controller
{
    public FileContentResult ImageOutput()
    {

         WebImage img = new WebImage("C:\\temp\\blank.jpg"));

         //Do whatever you want with the image using the WebImage class

         return new FileContentResult(img.GetBytes(), string.Format("image/{0}",img.ImageFormat));
    }
}

要使用它,只需按照达林所说的做同样的事情

<img src="<%= Url.Action("ImageOutput", "home") %>" alt="my image" />

This worked for me.

using System.Web.Helpers;
public class HomeController : Controller
{
    public FileContentResult ImageOutput()
    {

         WebImage img = new WebImage("C:\\temp\\blank.jpg"));

         //Do whatever you want with the image using the WebImage class

         return new FileContentResult(img.GetBytes(), string.Format("image/{0}",img.ImageFormat));
    }
}

To use it just do the same thing as Darin said

<img src="<%= Url.Action("ImageOutput", "home") %>" alt="my image" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文