在图片框中显示图标

Sri*_*ati 11 c# icons picturebox

我想icon file在一个图片框中显示.我正在使用此代码来设置图像.

pictureBox1.Image = new Icon(openFileDialog.FileName, new Size(48, 48)).ToBitmap();
Run Code Online (Sandbox Code Playgroud)

但我得到了这个例外.

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()
Run Code Online (Sandbox Code Playgroud)

如何克服这个问题?

谢谢.

Sri*_*ati 7

解决了这个问题.

pictureBox1.Image = Bitmap.FromHicon(new Icon(openFileDialog.FileName, new Size(48, 48)).Handle);
Run Code Online (Sandbox Code Playgroud)