public UploadThumb GetThumb(UploadedFile file, int width) { if (disposed) throw new ObjectDisposedException("UploadThumbGenerator"); if (!_thumbs.ContainsKey(file.TempKey)) _thumbs.Add(file.TempKey, new Dictionary<int, UploadThumb>()); var fileThumbs = _thumbs[file.TempKey]; if (fileThumbs.ContainsKey(width)) return (fileThumbs[width]); var thumb = new UploadThumb(); try { using (var image = Image.FromFile(file.TempFilePathAbsolute)) using (var resized = image.Width <= width ? image : ImageUtils.ResizeImage(image, width, false)) resized.Save(thumb.ThumbPathAbsolute, ImageFormat.Png); fileThumbs.Add(width, thumb); return thumb; } catch (OutOfMemoryException) { throw; } catch (Exception) { DeleteThumb(thumb); return null; } }
private void DeleteThumb(UploadThumb thumb) { if (File.Exists(thumb.ThumbPathAbsolute)) File.Delete(thumb.ThumbPathAbsolute); }