繁体   English   中英

如何在MVC3剃刀视图中显示存储在变量内的图像

[英]how do I display an image stored inside a variable in an MVC3 Razor View

我有一个项目,需要在其中显示本地存储在服务器上的图像的缩略图。 最初,我尝试使用jquery库只是为我的视图“缩略图化”原始图像。 但是,后来我意识到以这种方式提供潜在的成千上万张图像确实会占用带宽。 因此,下面是一种提取Windows缩略图的方法(仅按需提供一个全时图像)。

问题是,缩略图现在位于模型的变量内。 我不知道如何在“剃刀视图”中实际显示图像。 那么,如何在视图中显示System.Drawing.Icon项目?

public static System.Drawing.Icon GetThumbnailImages(string imageFilePath) 
        {
            ShellFile shellFile = ShellFile.FromFilePath(imageFilePath);
            Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
            System.Drawing.Icon shellThumb2 = shellFile.Thumbnail.Icon;
            //var shellThumb3 = shellFile.Thumbnail.MediumIcon;
            return shellThumb2;

        }

我会计算图像并在以动作为源的循环中生成标签

for(int i= 0; i< MyModel.ImageCount(); i++){

     <img src="@Url.Action("GetImage", "Home")" />


}

您的操作将需要发送回文件。 以此为例溢出

这将加载到图像中

我建议您使用新尺寸创建一个新文件,然后按url显示。 您可以使用类似:

public static string GenerateThumbnail(string filename, string target, int width)
{
    var img = Image.FromFile(filename);

    if (img.Width > width)
    {
        var bitmap = new Bitmap(width, GetTargetHeight(img.Width, width, img.Height));
        var g = Graphics.FromImage(bitmap);

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(img, 0, 0, width, GetTargetHeight(img.Width, width, img.Height));
        g.Dispose();

        using (Stream file = File.OpenWrite(target))
        {
            CopyStream(bitmap.ToStream(ImageFormat.Jpeg), target);
        }

        return target;
    }

    return filename;
}

private static int GetTargetHeight(int originalWidth, int targetWidth, int originalHeight)
{
    return Convert.ToInt32(decimal.Round((decimal)targetWidth / originalWidth * originalHeight, 0));
}

然后使用

<img src="@GenerateThumbnail("path", "targetFile", 100)" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM