nov 7, 2009

Enviado por en .NET, Windows

Usar una imagen como portada de carpeta en Windows

Portada de carpeta
Un tema corto, en Windows existen varias formas de tener una imagen como la portada de una carpeta, entre ellos están el crear un icono y añadirlo a desktop.ini, usar una opción (algo limitante) en versiones viejas del explorador. Existe una tercera el cual es tener un archivo Folder.jpg y AlbumArtSmall.jpg las cuales son usadas automáticamente por el explorador (y WMP, WMC, etc.) como portada de la carpeta.

El siguiente código (en C# para variar) emplea la tercera opción, se encarga de leer una imagen dada en la línea de comando del programa, crear una vista previa (con un borde añadido) y guardarlo como los archivos respectivos.

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);
}
}
}

A continuación, los archivos: FolderImage.cs y Program.cs

    Posts Relacionados

    1. Muy buen post man!
      Este no sabia ese uso de la funcion system.drawing! lo usare en 7 cuando tenga oportunidad

      pd: para enero se vienen cosas buenas, :D pero tendremos que esperar

      Usando Google Chrome 4.0.237.0 Google Chrome 4.0.237.0 en Windows XP Windows XP

    Dejar una respuesta

    Debes ser Alojarse para enviar un comentario.