Html帮助器标签如何动态显示图像

时间:2015-02-21 16:11:25

标签: asp.net-mvc-4

iam创建动态图像,但不能在标签img中显示 请指导我如何在View中动态设置路径。 这是我的代码

public class VisitCounterController : Controller
{
    // GET: VisitCounter
    public ActionResult Visit()
    {
        Response.Clear();
        Response.Cache.SetNoStore();
        Response.ContentType = "image/jpeg";
        int length = 6;
        int fontSize = 16;
        int letterWidth = 15;
        int letterHeight = 18;
        int letterOffsetX = 2;
        Color backgroundColor = Color.Black;
        int number = 100;
        string sNum = number.ToString().PadLeft(length, '0');
        Bitmap bmp = new Bitmap(letterWidth * length + 2, letterHeight + 2);
        Graphics gr = Graphics.FromImage(bmp);
        gr.CompositingQuality = CompositingQuality.HighQuality;
        gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.SmoothingMode = SmoothingMode.HighQuality;
        SolidBrush bgBrush = new SolidBrush(backgroundColor);
        gr.FillRegion(bgBrush, new Region(new Rectangle(0, 0, bmp.Width, bmp.Height)));
        Font font = new Font("Arial", fontSize, FontStyle.Bold, GraphicsUnit.Pixel);
        for (int i = 0; i <= length - 1; i++)
        {
            gr.DrawRectangle(Pens.LightGray, new Rectangle(i * letterWidth + 1, 1, letterWidth, letterHeight));
            gr.DrawString(sNum[i].ToString(), font, Brushes.White, i * letterWidth + letterOffsetX, 1);
        }

        gr.DrawRectangle(Pens.Gray, new Rectangle(0, 0, letterWidth * length          + 2, letterHeight + 2));
        MemoryStream ms = new MemoryStream();

         bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
         return File(ms.ToArray(), "image/png");


    }

iam使用此代码显示图片,但这不正确如何推动这个问题谢谢

<img src="~/Views/VisitCounter/Visit.cshtml" alt="Sample Image" width="300px" />

1 个答案:

答案 0 :(得分:0)

在此行中,您将引用视图本身而不是控制器操作。

<img src="~/Views/VisitCounter/Visit.cshtml" alt="Sample Image" width="300px" />

img src设置为@Url.Action("Visit", "VisitCounter")

<img src="@Url.Action("Visit", "VisitCounter")" alt="Sample Image" width="300px" />