I'm working on a program that involves changing the cursor image during a drag-drop event, at about 1:30 minutes of runtime or completing ~120 drag-drops the program crashes with the following error.
System.Runtime.InteropServices.ExternalException
HResult=0x80004005
Message=A generic error occurred in GDI+.
Source=System.Drawing
StackTrace:
at System.Drawing.Bitmap.GetHicon()
at Planner.Form1.dataGridView1_DragOver(Object sender, DragEventArgs e) in C:\Users\Name\Desktop\Items\Organizer\Planner\Planner\Form1.cs:line 319
at System.Windows.Forms.Control.OnDragOver(DragEventArgs drgevent)
at System.Windows.Forms.Control.System.Windows.Forms.IDropTarget.OnDragOver(DragEventArgs drgEvent)
at System.Windows.Forms.DropTarget.System.Windows.Forms.UnsafeNativeMethods.IOleDropTarget.OleDragOver(Int32 grfKeyState, POINTSTRUCT pt, Int32& pdwEffect)
Here is the code I'm using to change the cursor. When I first encountered the issue I added the try/catch to see if I could just skip over, this instead causes the program to deteriorate and break in other areas (without error, just lag and work improperly)
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
//MessageBox.Show(CursorID);
//try
{
Bitmap DragCursor = new Bitmap(myPath + CursorID + ".png");
DragCursor.MakeTransparent(Color.FromArgb(0, 0, 0, 0)); //TODO: This doesn't add transparency
Cursor cur = new Cursor(DragCursor.GetHicon());
Cursor.Current = cur;
DragCursor.Dispose();
}
//catch (Exception ex)
//{
//}
}
The drag-drop is functioning in a DataGridView of image and text cells (where only the image cells are visible). Each image has an associated ID that is placed in the right adjacent cell to track which image in present. When initiating the drag, the starting cell image is removed (and appears as the cursor) then when dropped, the target cell is held in memory, cursor image is dropped into the target and the initial cell is changed to the image now in holding (from the target). Just to help give an idea of what is going on. There are over 10,000 images in the folder.
User contributions licensed under CC BY-SA 3.0