public string CacheImage(Guid id, Image image) { var imageSet = GetOrCreateImageSet(id); lock (imageSet) { if (imageSet != null) { ClearImageSet(imageSet); } ImageInfo info = new ImageInfo(imageSet); info.Width = image.Width; info.Height = image.Height; info.ImageFormat = image.RawFormat.Equals(ImageFormat.MemoryBmp) ? ImageFormat.Png : image.RawFormat; //image was processed - may have transparency info.Date = DateTime.UtcNow; imageSet.PrimaryImage = info; try { using (var fs = ProtectedFileStream.OpenExclusiveWriter(info.Path)) { image.Save(fs, info.ImageFormat); } } catch { try { File.Delete(info.Path); } catch { //cleanup } // weird bug, some images on tvdb will not save as jpegs try { //Logger.ReportVerbose("Saving as png.."); info.ImageFormat = ImageFormat.Png; image.Save(info.Path, ImageFormat.Png); } catch { try { File.Delete(info.Path); } catch { //cleanup } // give up imageSet.PrimaryImage = null; throw; } } return info.Path; } }
private ImageInfo ResizeImage(ImageSet set, int width, int height) { lock (set) { ImageInfo info = new ImageInfo(set); info.Width = width; info.Height = height; info.Date = DateTime.UtcNow; info.ImageFormat = set.PrimaryImage.ImageFormat; set.ResizedImages.Add(info); using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(set.PrimaryImage.Path)) using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(width, height)) using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(newBmp)) { graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphic.DrawImage(bmp, 0, 0, width, height); MemoryStream ms = new MemoryStream(); newBmp.Save(ms, info.ImageFormat); using (var fs = ProtectedFileStream.OpenExclusiveWriter(info.Path)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(ms.ToArray()); } } return info; } }
public ImageInfo GetPrimaryImageInfo(Guid id) { var cmd = connection.CreateCommand(); cmd.CommandText = "select width, height, updated from images where guid = @guid order by width desc"; cmd.AddParam("@guid", id.ToString()); ImageInfo info = null; using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { info = new ImageInfo(null); info.Width = Convert.ToInt32(reader[0]); info.Height = Convert.ToInt32(reader[1]); info.Date = DateTime.Parse(reader[2].ToString()); } reader.Close(); } return info; }
private void AddToCache(IMediaLocation item) { if (item is IFolderMediaLocation) { return; } var extension = System.IO.Path.GetExtension(item.Path).ToLower(); ImageFormat imageFormat = null; if (extension == ".png") { imageFormat = ImageFormat.Png; } else if (extension == ".jpg") { imageFormat = ImageFormat.Jpeg; } else if (extension == ".gif") { imageFormat = ImageFormat.Gif; } else if (extension == ".bmp") { imageFormat = ImageFormat.Bmp; } else { // bad file in image cache File.Delete(item.Path); return; } var match = infoRegex.Match(item.Name); if (match == null || match.Groups[1].Value == null) { // bad file File.Delete(item.Path); return; } bool isPrimary = match.Groups[1].Value == "z"; Guid id = new Guid(match.Groups[2].Value); int width = -1; int height = -1; if (!string.IsNullOrEmpty(match.Groups[3].Value)) { width = Int32.Parse(match.Groups[3].Value); } if (!string.IsNullOrEmpty(match.Groups[4].Value)) { height = Int32.Parse(match.Groups[4].Value); } var imageSet = GetImageSet(id); if (imageSet == null) { imageSet = new ImageSet(this, id); imageInfoCache[id] = imageSet; } var info = new ImageInfo(imageSet); info.ImageFormat = imageFormat; info.Date = item.DateModified > item.DateCreated ? item.DateModified : item.DateCreated; //upgrade logic if (width == -1 || height == -1) { Image image = Image.FromFile(item.Path); isPrimary = true; info.Width = image.Width; info.Height = image.Height; imageSet.PrimaryImage = info; image.Dispose(); File.Move(item.Path, info.Path); } else { info.Width = width; info.Height = height; } if (isPrimary) { imageSet.PrimaryImage = info; } else { imageSet.ResizedImages.Add(info); } }
public string GetImagePath(Guid id) { ImageInfo info = GetPrimaryImageInfo(id); if (info == null) info = new ImageInfo(null); return GetImagePath(id, info.Width, info.Height); }