簡體   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