【问题标题】:Thumbnails with Asp.net Mvc带有 Asp.net Mvc 的缩略图
【发布时间】:2011-06-28 15:07:04
【问题描述】:

有没有人知道一种从字节数组中显示缩略图的方法,或者更好的是,一个库可以做到这一点。谢谢

【问题讨论】:

  • 我在我的表中添加了一个新列,它是另一个 blob,但这个是用于缩略图的,我使用下面发布的技术来缩小图像
  • 您正在寻找ImageResizing.Net 库。它是免费的,据我所知,它是最受欢迎的。

标签: asp.net-mvc image thumbnails


【解决方案1】:

有了 ASP.NET MVC 3 和 WebMatrix,我们现在有了很好的标准 WebImage 类,其中包括 GetImageFromRequest、Resize、Crop 和 AddTextWatermark 方法。

【讨论】:

    【解决方案2】:
    public ActionResult Thumbnail() {
                byte[] myByte = System.IO.File.ReadAllBytes(location);
                Image i;
                using (MemoryStream ms = new MemoryStream()) {
                    ms.Write(myByte , 0 , myByte.Length);
                    i = Image.FromStream(ms);
                }
                return File(imageToByteArray(i.GetThumbnailImage(100 , 100 , () => false , IntPtr.Zero)) , "image/jpeg");
            }
    
            public byte[] imageToByteArray ( System.Drawing.Image imageIn ) {
                MemoryStream ms = new MemoryStream();
                imageIn.Save(ms , System.Drawing.Imaging.ImageFormat.Gif);
                return ms.ToArray();
            }
    

    这是我用的。我不是每次想要缩略图时都这样做,而是在我的表中创建了一个新列,它是一个 varbinary,并在每次我想要缩略图时调用该列。

    【讨论】:

    • GetThumbnailImage() 会产生非常可怕的结果(尽管 GIF 编码无论如何都会使它毫无意义)。另请注意,您正在泄漏一个 Image 对象(GDI 句柄),不,GC 不会在您正确处理后清理它。 See this article
    【解决方案3】:

    我使用这个库来创建缩略图

    https://github.com/terjetyl/Simple.ImageResizer

    例子:

        [HttpPost]
        public ActionResult UploadFilePage(HttpPostedFileBase file, BannerCliente banner)
        {
            try
            {
                string filename = Path.GetFileName(file.FileName);
    
                string crearRutaThumb = Path.Combine(Server.MapPath("~/Carpeta/" + banner.ClienteId), "thumbnail");
                Directory.CreateDirectory(crearRutaThumb);
    
                string rutaImagenOriginal = Path.Combine(Server.MapPath("~/Carpeta/" + banner.ClienteId), filename);
    
                var frerf = new ImageResizer(byteFile(rutaImagenOriginal));
                frerf.Resize(100, ImageEncoding.Jpg100);
                frerf.SaveToFile(Path.Combine(Server.MapPath("~/Carpeta/" + banner.ClienteId + "/thumbnail"), file.FileName));
            }
            catch (Exception ex)
            {
    
                throw ex;
            }
    
            return View();
        }
    
        public byte[] byteFile(string fileName)
        {
            return System.IO.File.ReadAllBytes(fileName);
        }
    

    【讨论】:

      【解决方案4】:

      open-source ImageResizing.Net library 就是您要查找的内容

      它支持 SQL、S3 和文件系统图像,并允许调整大小、裁剪、旋转和许多其他操作。

      如果您想生成“虚拟图像”而不是简单地修改现有图像,它还提供 IVirtualImageProvider 接口。

      注意:SQL 和 S3 插件是开源的,但需要 99 美元的下载费(包括两者)。核心库是免费的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多