wpf中使用image控件,显示图片,如何让图片的透明区域不响应鼠标事件(有颜色区域照常响应)。

解决方案 »

  1.   

    仅供参考
    // 公共变量
    System.Drawing.Bitmap bitmap = null;// Window_Loaded 事件代码
    // 前提 img.Source 是文件名或类似application://... 的资源字符串Uri uri = new Uri(img.Source.ToString());// 256 是我加载的 png 图片的大小
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.DecodePixelWidth = 256;
    bi.DecodePixelHeight = 256;
    bi.UriSource = uri;
    bi.EndInit();var memory = new System.IO.MemoryStream();
    BitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bi));
    encoder.Save(memory);bitmap = new System.Drawing.Bitmap(memory);// 根据像素颜色进行判断吧
    // 当然 如果图像发生了缩放 则需要进行重新计算
    private void img_MouseMove(object sender, MouseEventArgs e)
    {
        int x = (int)(e.GetPosition(img).X);
        int y = (int)(e.GetPosition(img).Y);    Console.WriteLine("{0}, {1}", x, y);    try
        {
            // 此处需要进行 x, y 是否超出图片大小的判断
            // 我就偷懒了
            System.Drawing.Color c = bitmap.GetPixel(x, y);
            Color cl = Color.FromArgb(c.A, c.R, c.G, c.B);
            rect.Fill = new SolidColorBrush(cl);
        }
        catch
        {
        }
    }
      

  2.   

    http://stackoverflow.com/questions/3592404/irregular-png-buttons-click-event-in-wpf/3594911#3594911