jeudi 17 avril 2014

WinForms - c# - erreur affichage Icon dans picturebox - Stack Overflow


I've been using this code to show an icon in a picturebox.


Image FromIcon(Icon ico)
{
try
{
this.toolTip1.SetToolTip(pictureBox1, "The icon of the Executable");
return ico.ToBitmap();
}
catch (Exception e)
{
this.toolTip1.SetToolTip(pictureBox1, "Don't worry, it looks perfectly fine on the executable!");
MessageBox.Show(e.Message + "\r\n" + e.StackTrace);
Clipboard.SetText(e.Message + "\r\n" + e.StackTrace);

// Alternate method
return Bitmap.FromHicon(ico.Handle);
}
}

However it is showing this error.


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

Also the icon is shown in a nasty way,


enter image description here


That's the same icon I use for my application. What can go wrong?


That icon is 32 bit as well as the other.


enter image description here


If I use another icon, it works fine and no error pops up.


enter image description here


Thanks.



I've been using this code to show an icon in a picturebox.


Image FromIcon(Icon ico)
{
try
{
this.toolTip1.SetToolTip(pictureBox1, "The icon of the Executable");
return ico.ToBitmap();
}
catch (Exception e)
{
this.toolTip1.SetToolTip(pictureBox1, "Don't worry, it looks perfectly fine on the executable!");
MessageBox.Show(e.Message + "\r\n" + e.StackTrace);
Clipboard.SetText(e.Message + "\r\n" + e.StackTrace);

// Alternate method
return Bitmap.FromHicon(ico.Handle);
}
}

However it is showing this error.


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

Also the icon is shown in a nasty way,


enter image description here


That's the same icon I use for my application. What can go wrong?


That icon is 32 bit as well as the other.


enter image description here


If I use another icon, it works fine and no error pops up.


enter image description here


Thanks.


0 commentaires:

Enregistrer un commentaire