继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

silverlight:如何在图片上挖个洞

一只斗牛犬
关注TA
已关注
手记 338
粉丝 49
获赞 300

一、不写代码的方法:用Blend

看图说话:

这是待处理的图片win7
https://img4.mukewang.com/5af5582e0001e90b05190343.jpg

在win7上,画一个矩形,再用钢笔随便画个封闭的path
https://img.mukewang.com/5af558380001f9d005220352.jpg

将矩形与path合并组成复杂的路径
https://img2.mukewang.com/5af55845000168e006630692.jpg

将合成后的复杂路径与win7图片同时选中,然后生成剪切路径
https://img3.mukewang.com/5af558520001ac3a06580666.jpg

这样我们就得到了一个不规则的图片轮廓(当然这里演示的去掉不规则部分,反过来就是挖洞)
https://img2.mukewang.com/5af5585d0001c23405260347.jpg

 

二、用代码挖洞

原理:先用WriteableBitmap把原图片复制一份,然后将原图隐藏,接下来把指定区域的象素透明度指定为0 

代码:


<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="Mark.Index"
    d:DesignWidth="400" d:DesignHeight="250" Width="400" Height="250" >
    <Grid x:Name="LayoutRoot">        
        <Image x:Name="win7" Source="img/win7.jpg" Width="400" Height="250" d:IsHidden="True"/>
        <Image x:Name="imgMask" d:IsHidden="True" ></Image>        
    </Grid>
</UserControl>

 

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace Mark
{
    public partial class Index : UserControl
    {
        public Index()
        {
            // Required to initialize variables
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Index_Loaded);
        }
        void Index_Loaded(object sender, RoutedEventArgs e)
        {
            WriteableBitmap wb = new WriteableBitmap(win7, null);
            this.imgMask.Source = wb;
            win7.Visibility = Visibility.Collapsed;
            GenMask(wb);
        }
        void GenMask(WriteableBitmap wb)
        {            
            int _width = (int)win7.Width;
            int _height = (int)win7.Height;
            #region 把四周边距50px以内的区域挖空
            int _padding = 50;
            
            for (int row = _padding + 1; row < _height - _padding; row++)
            {
                for (int i = _width * row + _padding; i < _width * (row+1) - _padding; i++)
                {
                    wb.Pixels[i] = BitConverter.ToInt32(new byte[] { 0, 0, 0, 0 }, 0);//注意顺序:byte数组的含义依次为 {b,g,r,a},即:{蓝,绿,红,透明度}
                }               
            }
            #endregion
        }
    }
}

效果:
https://img4.mukewang.com/5af558b300014b3004730291.jpg

利用这个还能玩点花样(在指定区域添加白色噪点)。

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP