2010-02-01 12 views

risposta

7

Un .ico file di può avere più immagini in essa, ma quando si carica un file ico e crea un oggetto icona, solo una di quelle immagini viene caricato. Windows sceglie l'immagine più appropriata in base alla modalità di visualizzazione corrente e alle impostazioni di sistema e utilizza quella per inizializzare l'oggetto System.Drawing.Icon, ignorando gli altri.

Quindi non è possibile creare uno System.Drawing.Icon con più immagini, è necessario selezionarne uno prima di creare l'oggetto Icon.

Ovviamente, è possibile creare un'icona in fase di esecuzione da una bitmap, è quello che stai veramente chiedendo? O stai chiedendo come creare un file .ico?

+1

Ma se caricare un file ico come ho mostrato in precedenza, posso accedere al 16x16, 32x32, 48x48, ecc come questo -: Icona smallIcon = new Icon (myIcon, 16, 16); – Damien

+2

Perché sa da quale file/risorsa è stata creata l'icona e può tornare ad essa e ottenere una nuova immagine. Non c'è modo di AGGIUNGERE un'immagine ad un oggetto Icon. –

+0

@JohnKnoeller In realtà, non è proprio vero. Tutti i dati dell'immagine vengono caricati dalla classe 'Icon' in memoria e quando si ridimensiona da un altro oggetto' Icon', i dati dell'immagine vengono condivisi e le dimensioni più vicine vengono estratte dai dati originali (condivisi). – NetMage

0

C'è un bel code snippet here. Utilizza il metodo Icon.FromHandle.

Dal link:

/// <summary> 
/// Converts an image into an icon. 
/// </summary> 
/// <param name="img">The image that shall become an icon</param> 
/// <param name="size">The width and height of the icon. Standard 
/// sizes are 16x16, 32x32, 48x48, 64x64.</param> 
/// <param name="keepAspectRatio">Whether the image should be squashed into a 
/// square or whether whitespace should be put around it.</param> 
/// <returns>An icon!!</returns> 
private Icon MakeIcon(Image img, int size, bool keepAspectRatio) { 
    Bitmap square = new Bitmap(size, size); // create new bitmap 
    Graphics g = Graphics.FromImage(square); // allow drawing to it 

    int x, y, w, h; // dimensions for new image 

    if(!keepAspectRatio || img.Height == img.Width) { 
    // just fill the square 
    x = y = 0; // set x and y to 0 
    w = h = size; // set width and height to size 
    } else { 
    // work out the aspect ratio 
    float r = (float)img.Width/(float)img.Height; 

    // set dimensions accordingly to fit inside size^2 square 
    if(r > 1) { // w is bigger, so divide h by r 
     w = size; 
     h = (int)((float)size/r); 
     x = 0; y = (size - h)/2; // center the image 
    } else { // h is bigger, so multiply w by r 
     w = (int)((float)size * r); 
     h = size; 
     y = 0; x = (size - w)/2; // center the image 
    } 
    } 

    // make the image shrink nicely by using HighQualityBicubic mode 
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
    g.DrawImage(img, x, y, w, h); // draw image with specified dimensions 
    g.Flush(); // make sure all drawing operations complete before we get the icon 

    // following line would work directly on any image, but then 
    // it wouldn't look as nice. 
    return Icon.FromHandle(square.GetHicon()); 
} 
+3

Credo che voglia creare un'icona con più immagini ... –

0

Si potrebbe provare a utilizzare png2ico per creare un file .ico, invocandolo utilizzando System.Diagnostics.Process.Start. Dovresti creare le tue immagini e salvarle su disco prima di invocare png2ico.