【问题标题】:Displaying an icon in a picturebox在图片框中显示图标
【发布时间】:2013-04-03 08:55:46
【问题描述】:

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

如何解决这个问题?

谢谢。

【问题讨论】:

    标签: c# icons picturebox


    【解决方案1】:

    解决了这个问题。

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

    【讨论】:

      【解决方案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);
          }
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2018-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-01
          • 1970-01-01
          • 2012-08-05
          相关资源
          最近更新 更多