Does anyone know of a way to display a thumbnail image from a byte array, or even better, a library that does this. Thanks
-
I added a new column to my table that is another blob but this one is for thumbnails and i use the technique i posted below to shrink the imageCollin O'Connor– Collin O'Connor2011-02-17 20:54:44 +00:00Commented Feb 17, 2011 at 20:54
-
You're looking for the ImageResizing.Net library. It's free, and from what I know, the most popular.Lilith River– Lilith River2011-10-15 22:41:48 +00:00Commented Oct 15, 2011 at 22:41
4 Answers
With ASP.NET MVC 3 and WebMatrix we now have nice standard WebImage class which has among others GetImageFromRequest, Resize, Crop and AddTextWatermark methods.
Comments
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();
}
This is what i used. Instead of doing this each time i want a thumbnail, i created a new column in my table that was a varbinary and called that column each time i wanted a thumbnail.
1 Comment
I used this library to create thumbnail
https://github.com/terjetyl/Simple.ImageResizer
Example :
[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);
}
Comments
The open-source ImageResizing.Net library is what you're looking for
It supports SQL, S3, and filesystem images, and allows resizing, cropping, rotation and many other manipulations.
It also offers an IVirtualImageProvider interface if you want to generate 'virtual images' instead of simply modifying existing ones.
NOTE: The SQL and S3 plugins are open-source, but have a $99 download fee (which includes both). The core library is free.