WPF图像工具提示边缘检测

时间:2013-04-02 23:10:42

标签: wpf image tooltip edge-detection

我对WPF有点新意,但我遇到了一个问题,我正试图在带有背景图像的图像或按钮上放置工具提示。基本上我想要做的是我希望工具提示只显示图像实际存在的位置而不是透明度。这是一个问题,因为当按钮或图像使用存储它的矩形时,工具提示也将显示在该区域内。

这是一个示例按钮(忽略糟糕的设计,但我需要一个复杂的形状来给你一个想法)

Exmaple:1

这是图像/按钮的边缘

Exmaple:1

Printscreen没有捕获鼠标,但想象一下鼠标实际上没有悬停在形状上并且它位于图像框/按钮的区域内。

Exmaple:1 http://i1109.photobucket.com/albums/h426/Melkirth/Imagearea.png

这是我实际代码的一个小例子

    <Button Height="160" Width="240" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
        <Button.Background>
            <ImageBrush ImageSource="Images/Cloud1.png"></ImageBrush>
        </Button.Background>
        <Button.ToolTip>
            <TextBlock Margin="10" FontSize="14">Click me to begin your test</TextBlock>
        </Button.ToolTip>
    </Button>

1 个答案:

答案 0 :(得分:1)

取自this answer to a similar question

专业图像类:

public class OpaqueClickableImage : Image
    {
        protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
        {
            var source = (BitmapSource)Source;
            var x = (int)(hitTestParameters.HitPoint.X / ActualWidth * source.PixelWidth);
            var y = (int)(hitTestParameters.HitPoint.Y / ActualHeight * source.PixelHeight);
            var pixels = new byte[4];
            source.CopyPixels(new Int32Rect(x, y, 1, 1), pixels, 4, 0);
            if (pixels[3] < 10) return null;
            return new PointHitTestResult(this, hitTestParameters.HitPoint);
        }
    }

XAML:

<Window x:Class="MiscSamples.ImageButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MiscSamples"
        Title="ImageButton" Height="300" Width="300">
    <Grid Background="Green">
        <Button VerticalAlignment="Center" HorizontalAlignment="Center"
                ToolTip="Hello!!">
            <Button.Template>
                <ControlTemplate>
                    <local:OpaqueClickableImage Source="./Resources/SomeImage.png"/>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </Grid>
</Window>

只有当鼠标位于图像的非透明区域时,才会显示工具提示。

相关问题