简体   繁体   中英

How do I get Image of Control on Click in C#?

I want to get exact that Control Image on which I click.

Like If I clicked on Save as option.I should get the save as control Image.

Here is Image Example Which I want to achieve. First I clicked on File Menu then Save as.
I want to achieve this

我想实现这一目标

Currently. I am getting the image on clicked base of coordinates of Click. Like this.I am getting Image on Click base of Click Coordinates.This is not what I want to do. 我在点击坐标的点击基上获取图像。这不是我想要做的。

Another Example. I clicked on Stack over flow questions control. I should get this result. 另一个例子

My code.

 if (ClientRectangle.Contains(PointToClient(Control.MousePosition)))
        {

           // MessageBox.Show("Width:" + ClientRectangle.Width.ToString() + "--- Height: " + ClientRectangle.Height.ToString() + "---" + "X:" + ClientRectangle.X.ToString() + "--- Y: " + ClientRectangle.Y.ToString());

            Point location = button1.PointToScreen(Point.Empty);

            MessageBox.Show(location.X.ToString()+"---------"+location.Y.ToString()+"---------"+ location+ClientRectangle.Width.ToString()+"---------"+ClientRectangle.Height.ToString());
    //         Rectangle rect = new Rectangle(0, 0, 100, 100);
            int with = ClientRectangle.Width;
            int height = ClientRectangle.Height;
    Bitmap bmp = new Bitmap((int)with, (int)height, PixelFormat.Format32bppArgb);
    Graphics g = Graphics.FromImage(bmp);
   // g.CopyFromScreen(ClientRectangle.Left, ClientRectangle.Top, 50,50, bmp.Size, CopyPixelOperation.SourceCopy);
    g.CopyFromScreen(location.X, location.Y, 0, 0, new Size(ClientRectangle.Width-30, ClientRectangle.Height-36));
    bmp.Save(@"capture.jpg", ImageFormat.Jpeg)

From what i understood from your question, you want to show the image of the menu option on mouse pointer when mouse is pointed/clicked on that particular menu item. If that's so you can do this by changing Cursor:

System.Windows.Resources.StreamResourceInfo sri = Application.GetResourceStream(new Uri("/Images/handClosed.cur", UriKind.RelativeOrAbsolute));
Cursor customCursor = new Cursor(sri.Stream);
Mouse.OverrideCursor = customCursor;

Here, "/Images/handClosed.cur" is a locally stored image in the project. You can store the images for all the menu item and show them like this based on the menu item.

Edit: This is a WPF Example


Check this Simple Magnifier app. Reference the source and to get the desired effect on mouse click.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM