在Asp.Net Mvc中获取字节[]图像,调整大小和显示

时间:2016-10-02 09:54:45

标签: c# asp.net asp.net-mvc entity-framework razor

这是我的模特课

public class News: Public
{
    [Key]
    public int NewsID { get; set; }

    [Required]
    [StringLength(100, MinimumLength = 2)]
    public string Title { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

    public DateTime NewsDate { get; set; }

    [Required]

    [DataType(DataType.MultilineText)]
    [StringLength(1000, MinimumLength = 2)]
    public string NewsTXT { get; set; }

    public byte[] NewsPic { get; set; }
}

我在create控制器中使用它将Byte[]中的图片保存到数据库

using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
    news.NewsPic = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}

我想在我的控制器中制作图像拇指并在视图中显示,并且不想在任何地方保存拇指。

我想做类似的事情,但是从数据库中获取图片

缩略图控制器

 public ActionResult Thumbnail(int width, int height){
var imageFile = Path.Combine(Server.MapPath("~/app_data"), "test.png");
using (var srcImage = Image.FromFile(imageFile))
using (var newImage = new Bitmap(width, height))
using (var graphics = Graphics.FromImage(newImage))
using (var stream = new MemoryStream())
{
    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphics.DrawImage(srcImage, new Rectangle(0, 0, width, height));
    newImage.Save(stream, ImageFormat.Png);
    return File(stream.ToArray(), "image/png");
}

}

在视图中

<img src="@Url.Action("Thumbnail", "SomeController", new { width = 100, height = 50 })" alt="thumb" />

这个代码的版本是,在拇指控制器中是:

 public ActionResult Thumbnail(int width, int height,int id)
    {
        // TODO: the filename could be passed as argument of course
        var photo = db.News.Find(id).NewsPic;
        string imageBase64 = Convert.ToBase64String(photo);
        var imageSrc = string.Format("data:image/jpg;base64,{0}", imageBase64);

        using (var srcImage = Image.FromFile(imageSrc))
        using (var newImage = new Bitmap(width, height))
        using (var graphics = Graphics.FromImage(newImage))
        using (var stream = new MemoryStream())
        {
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics.DrawImage(srcImage, new Rectangle(0, 0, width, height));
            newImage.Save(stream, ImageFormat.Png);
            return File(stream.ToArray(), "image/png");
        }
    }

我的版本,在视图中:

        <img src="@Url.Action("Thumbnail", "News", new { width = 100, height = 50,id=Model.NewsID })" alt="thumb" />

但非常有效

1 个答案:

答案 0 :(得分:1)

public ActionResult Thumbnail(int width, int height, int id){
        // TODO: the filename could be passed as argument of course
        var photo = db.News.Find(id).NewsPic;
        var base64 = Convert.ToBase64String(photo);
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64);
        MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        Image image = Image.FromStream(ms, true);

        using (var newImage = new Bitmap(width, height))
        using (var graphics = Graphics.FromImage(newImage))
        using (var stream = new MemoryStream())
        {
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics.DrawImage(image, new Rectangle(0, 0, width, height));
            newImage.Save(stream, ImageFormat.Png);
            return File(stream.ToArray(), "image/png");
        }

    }