using System; using System.IO; using System.Drawing; using System.Drawing.Imaging; namespace Utilities.SetFileAsFolderPicture { class FolderImage { string folder_path; int border_width; Color border_color; public FolderImage(string path) { folder_path = path; border_width = 0; } public void SetBorder(int width, Color color) { border_width = width; border_color = color; } public void SetImage(Image image) { string albumart_path = Path.Combine(folder_path, "Folder.jpg"); string smallart_path = Path.Combine(folder_path, "AlbumArtSmall.jpg"); Size albumart_size = GetSize(image.Size, 200); Size smallart_size = GetSize(image.Size, 75); Save(image, albumart_size, albumart_path); Save(image, smallart_size, smallart_path); } private Size GetSize(Size image, int size) { int width = image.Width, height = image.Height; if (width > height) { return new Size(width * (size - border_width) / height + border_width, size); } else { return new Size(size, height * (size - border_width) / width + border_width); } } private void Save(Image image, Size size, string path) { int width = size.Width, height = size.Height; Bitmap output = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(output); graphics.DrawImage(image, border_width, border_width, width - border_width * 2, height - border_width * 2); graphics.DrawRectangle(new Pen(border_color, border_width), 0, 0, width, height); output.Save(path, ImageFormat.Jpeg); File.SetAttributes(path, FileAttributes.Hidden | FileAttributes.System); } } }