简体   繁体   中英

How to match X and Y coordinates from a click inside a button with the corresponding bitmap pixels (C# Windows Store Apps)?

I have a button with some image as its content. I attach PointerEntered event handler to the button and get PointerPoint coordinates when the cursor enters the button. I can get the bitmap of the button content using RenderTargetBitmap and get an array of pixels from that bitmap . I need to find the exact pixel corresponding to the PointerPoint location to check it for transparency.

Here is the code:

public async void button_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    Button button = sender as Button;
    RenderTargetBitmap bitmap;
    bitmap = new RenderTargetBitmap();
    await bitmap.RenderAsync(button);

    IBuffer pixelBuffer = await bitmap.GetPixelsAsync();
    byte[] pixels = pixelBuffer.ToArray();

    Pointer pointer = e.Pointer;
    PointerPoint ppt = e.GetCurrentPoint(button);

}

I have ppt.X and ppt.Y , how to I find the corresponding pixel in pixels array?

要访问1d数组中的某个2d像素,偏移量将为x + y * width,其中width是图像的宽度

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