Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi good people,

I am working on a little program that contains two pictureBoxes where the first, pictureBox1, shows an original image and the other, pictureBox2, will show a zoomed version of a selected portion of pictureBox1 that is enclosed by a magnifying rectangle. I looked for a solution in CP and I found the one linked below:

Found Article Link

However, this solution didn't work for me as the event handler passes EventArgs not a MouseEventArgs that the method (UpdateZoomedImage) in the above solution uses.


Any ideas or workarounds of how I can get that method to work with EventArgs being passed to the event handler shown below?

C#
private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            UpdateZoomedImage(e);
        }


Error I am getting is this:

Argument 1: cannot convert from 'System.EventArgs' to 'System.Windows.Forms.MouseEventArgs'

Thanks!
Posted
Updated 26-Dec-14 8:39am
v2
Comments
Richard MacCutchan 26-Dec-14 15:19pm    
Post your question in the forum at the end of the article, so the author sees it.
BillWoodruff 27-Dec-14 2:08am    
Is using the "zoom" feature of the PictureBox here working as you expect it: is that an issue ?

Is it apparent that an object of the type System.EventHandler cannot be cast to System.Windows.Forms.MouseEventArgs. (You did not specify anything properly in your question, did not reference the event, but it's most likely that you mean the event System.Windows.Forms.Control.MouseHover which is uses with the event arguments type System.EventHandler. Next time, provide full names of relevant types, indicate what UI library you are using.)

Please see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs%28v=vs.110%29.aspx[^].

Your approach is wrong from the very beginning. You cannot get all the information, say, one the mouse position, from this event. You need to use, additionally or instead of this one, other mouse events working with the class System.Windows.Forms.MouseEventArgs, such as MouseMove. It's your responsibility to provide appropriate logic — you did not show what your UpdateZoomedImage does.

But you are making much worse design mistake: the use of PictureBox for something like zooming. This control is redundant, wasteful and not useful at all, unless you use if for something much simpler, such as showing some static images. I explain it all in detail and suggest what to do in my past answer; the first one is specifically focused on zooming. Please see:
Zoom image in C# .net mouse wheel[^];
why not using PictureBox and what to do instead:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^];
graphics rendering:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

—SA
 
Share this answer
 
v2
Comments
Ayman Abdullah 27-Dec-14 2:59am    
Hey there,

Thanks for writing an answer, but I just wanted to make sure you clicked the link where it says "Found Article Link". That can show you the whole code for the method (UpdateZoomedImage). I'll look forward to read more comments from you.
Sergey Alexandrovich Kryukov 27-Dec-14 6:20am    
And what's the problem? What information from event arguments did you plan to use, if it did not miss?
—SA
If you need to get the Mouse Position at any time in WinForms, you can use:
C#
Point currentPoint = MousePosition;
That will return where the Mouse is in screen co-ordinates.

To get from screen co-ordinates to current Form co-ordinates:
C#
Point currentFormPoint = this.PointToClient(MousePosition)
To get the co-ordinates in a specific Control, like a PictureBox:
C#
Point currentControlPoint = pictureBox1.PointToClient(MousePosition);
 
Share this answer
 
Comments
Ayman Abdullah 27-Dec-14 3:46am    
Thanks for your answer!

At least I progressed one step forward using your tips.
BillWoodruff 27-Dec-14 3:50am    
Glad this was useful !
try this:
C#
private void pictureBox1_MouseHover(object sender, MouseEventArgs e)
{
    UpdateZoomedImage(e);
}

you probably aren't using the right type of event args for the mouse event, thus the system raises the error it gives you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Dec-14 16:16pm    
No, if this is about the event MouseHover, its event arguments parameter is of type System.EventHandler. It is not and is not derived from MouseEventArgs. Your advice cannot work.
—SA
MasterCodeon 26-Dec-14 16:51pm    
ah i didn't realize that he was using the MouseHover event(making a huge face palm because i could have figured that out from the name of the method)
sorry

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900