Solved

GetPixel at Mouse Position

Posted on 2013-11-27
4
845 Views
Last Modified: 2014-11-12
I'm trying to find the color of the pixel under the mouse pointer in vb.net (Windows Phone 8)

I think the way forward is to create a WriteableBitmap of the image on the page and use .GetPixel(GetPosition(mImage).X, GetPosition(mImage).y)

Problem is that the image on the page is set to fill the page.  How can I translate the position in the stretched image to that of the unstreched image ?
0
Comment
Question by:Dodsworth
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39683593
Here is an example, that uses a Border class, the MouseMove event, and e.GetPosition to get the mouse position as a Point.

Track Mouse Position in WPF UIElement
http://www.codeproject.com/Tips/243314/Track-Mouse-Position-in-WPF-UIElement

There is a CodePlex project, called WriteableBitmapEx, that provides extension methods for the WriteableBitmap class to make it easier to work with that class:

WriteableBitmapEx
http://writeablebitmapex.codeplex.com/
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39689485
hi.. this code is in C# please convert into vb

private bool TrackHorizontally = false;
        this.border.MouseMove += new MouseEventHandler(border_MouseMove);
        void border_MouseMove(object sender, MouseEventArgs e)
        {
 
            var cBegin = Colors.Green;
            var cEnd = Colors.Yellow;
            GradientStop gs1, gs2;
            var brush = new LinearGradientBrush();
            var gs0 = new GradientStop(cBegin, 0);
            var gs3 = new GradientStop(cEnd, 1.0);
 
            Point p = e.GetPosition(this.border);
            var factor = TrackHorizontally ? p.Y / this.border.Height : p.X / this.border.Width;
 
            if (TrackHorizontally)
            {
                brush.StartPoint = new Point(0.5, 0);
                brush.EndPoint = new Point(0.5, 1);
 
                gs1 = new GradientStop(cBegin, factor);
                gs2 = new GradientStop(cEnd, factor);
            }
            else
            {
                brush.StartPoint = new Point(0, 0.5);
                brush.EndPoint = new Point(1, 0.5);
 
                gs1 = new GradientStop(cBegin, factor);
                gs2 = new GradientStop(cEnd, factor);
            }
 
            brush.GradientStops = new GradientStopCollection { gs0, gs1, gs2, gs3 };
 
            this.border.Background = brush;
        }
    }
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39702920
I know how to get the mouse position and pixel color.. just not how to scale the on screen image to the in memory image.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39703029
OK, I missed a very crucial part of your question:

"How can I translate the position in the stretched image to that of the unstretched image ?"

Off the top of my head, I would say that you need to determine a conversion factor...

Example:

Image 1 = 800 x 600
Image 2 = 400 x 300

Image 2 is half the height and width of Image 1.  That means that for every pixel in Image 2, there are twice as many pixels as Image 1.  

Conversion factor from #2 to #1 equals 2
Conversion factor from #1 to #2  equals .5
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question