2013-04-03 20 views
9

icon fileを画像ボックスに表示しようとしています。私はこのコードを使ってイメージを設定しています。ピクチャボックスにアイコンを表示

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

しかし、この例外が発生しています。

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() 

この問題を解決するにはどうすればよいですか?

ありがとうございました。

答えて

4

問題を解決しました。

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

これを試してみてください。

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

は、このヘルプを願っています。

+0

私が見つけた何が。とにかくありがとう。 –

2

アイコンの中には、48x48〜32x32のサイズが正しくないものがあります。

私の最終的なコードは次のとおりです。

Bitmap _image; 
    try 
    { 
    _image = new Icon(icon, width, height).ToBitmap(); 
    } 
    catch(ArgumentOutOfRangeException) 
    { 
    _image = Bitmap.FromHicon(new Icon(icon, new Size(width, height)).Handle); 
    } 
関連する問題