2013-04-03 20 views
9

Sto tentando di visualizzare icon file in una finestra di immagine. Sto usando questo codice per impostare l'immagine.Visualizzazione di un'icona in una casella immagine

pictureBox1.Image = new Icon(openFileDialog.FileName, new Size(48, 48)).ToBitmap(); 

Ma sto ottenendo questa eccezione.

System.ArgumentOutOfRangeException: Requested range extends past the end of the array. 
    at System.Runtime.InteropServices.Marshal.CopyToNative(Object source, Int32 startIndex, IntPtr destination, Int32 length) 
    at System.Runtime.InteropServices.Marshal.Copy(Byte[] source, Int32 startIndex, IntPtr destination, Int32 length) 
    at System.Drawing.Icon.ToBitmap() 

Come superare questo problema?

Grazie.

risposta

4

Risolto il problema.

pictureBox1.Image = Bitmap.FromHicon(new Icon(openFileDialog.FileName, new Size(48, 48)).Handle); 
4

Try This:

pictureBox1.Image = Bitmap.FromHicon(new Icon(openFileDialog.FileName, new Size(48, 48)).Handle); 

Hope Questa Guida.

+0

Questo è quello che ho trovato. Grazie comunque. –

2

Alcune icone sono state ridimensionate in modo errato da 48x48 a 32x32.

Il mio codice finale è:

Bitmap _image; 
    try 
    { 
    _image = new Icon(icon, width, height).ToBitmap(); 
    } 
    catch(ArgumentOutOfRangeException) 
    { 
    _image = Bitmap.FromHicon(new Icon(icon, new Size(width, height)).Handle); 
    }