Solved

GetPixel at Mouse Position

Posted on 2013-11-27
4
811 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now