在图片框中显示图标

时间:2013-04-03 08:55:46

标签: c# icons picturebox

我想在图片框中显示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()

如何克服这个问题?

感谢。

3 个答案:

答案 0 :(得分:7)

解决了这个问题。

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

答案 1 :(得分:3)

某些图标的大小不正确48x48到32x32。

我的最终代码是:

localhost/furbook.com/public/cats

答案 2 :(得分:0)

有时Bitmap.FromHicon无法完美转换。我找到了另一个解决方案:

// event Paint of pictureBox1
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawIcon(theIcon, 0, 0);
}
相关问题