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

本文关键字:显示 Asp Mvc 字节 图像 获取 调整 | 更新日期: 2023-09-27 18:11:20

这是我的模型class

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");
        }
    }

my version, in view:

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

但不工作

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

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");
        }
    }