一天一天学 windows phone 控件 之 Image + InkPresenter(十四)

本文介绍了Windows Phone 8中关于Image控件和InkPresenter的使用。Image控件在容器中显示图片,以适应不同尺寸需求;InkPresenter作为绘图控件,常用于实现画图功能。通过示例代码展示了如何在页面上应用这两个控件,提供源码下载供读者实践。
摘要由CSDN通过智能技术生成

废话:因为周末 一老同学从外地来,陪了别人两天,顺便看了场演唱会,再送了下。就没时间更新了。

我们接着讲控件,先简单把Image控件说下,然后我们来说下绘图控件InkPresenter。

Image控件如果我们仅仅只是简单放置一个图片路径,那么我们看到的是当前容器大小的图。所以如果我们要显示更大的尺寸,我们需要在Image控件外套一个容器控件。页面的代码如下:

<Grid x:Name="ContentPanel"
              Grid.Row="1"
              Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="4*" />
                <RowDefinition Height="4*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <ScrollViewer Grid.Row="0"
                          Margin="15"
                          HorizontalScrollBarVisibility="Auto"
                          VerticalScrollBarVisibility="Auto">
                <Image Source="Image/1.jpg" Stretch="None" />
            </ScrollViewer>
            <Image Grid.Row="1"
                   Margin="15"
                   Source="Image/1.jpg"
                   Stretch="Uniform" />
            <HyperlinkButton Grid.Row="2"
                             Content="go to InkPresenter Page"
                             NavigateUri="/Page1.xaml" />
        </Grid>

如果如图

最后加了跳转到InkPresenter 控件的链接。我们添加InkPresenter这个控件就是我们常看到的画图控件,现在很多修改图片的软件应该就是这个控件的基础上改吧改吧就行了。页面代码如下:

<Grid x:Name="ContentPanel"
              Grid.Row="0"
              Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <InkPresenter Name="inkPrt"
                          Grid.Row="0"
                          Width="420"
                          Height="600"
                          Background="Violet"
                          LostMouseCapture="inkPrt_LostMouseCapture"
                          MouseLeftButtonDown="inkPrt_MouseLeftButtonDown"
                          MouseMove="inkPrt_MouseMove">
                <InkPresenter.Clip>
                    <RectangleGeometry Rect="0,0,420,600" />
                </InkPresenter.Clip>
            </InkPresenter>
            <StackPanel Grid.Row="1" Orientation="Horizontal">
                <RadioButton Checked="RadioButton_Checked">
                    <RadioButton.Content>
                        <Rectangle Width="30"
                                   Height="30"
                                   Fill="Blue" />
                    </RadioButton.Content>
                </RadioButton>
                <RadioButton Checked="RadioButton_Checked">
                    <RadioButton.Content>
                        <Rectangle Width="30"
                                   Height="30"
                                   Fill="Yellow" />
                    </RadioButton.Content>
                </RadioButton>
                <RadioButton Checked="RadioButton_Checked">
                    <RadioButton.Content>
                        <Rectangle Width="30"
                                   Height="30"
                                   Fill="Green" />
                    </RadioButton.Content>
                </RadioButton>
                <RadioButton Checked="RadioButton_Checked">
                    <RadioButton.Content>
                        <Rectangle Width="30"
                                   Height="30"
                                   Fill="Red" />
                    </RadioButton.Content>
                </RadioButton>
            </StackPanel>

        </Grid>

代码段

//按住到松开 的一笔
        Stroke MyStroke = null;
        //笔的外观
        DrawingAttributes myDrawing = null;
        //笔的粗细
        double strokeWeight = 10;
        public Page1()
        {
            InitializeComponent();
            //初始化画笔
            myDrawing = new DrawingAttributes();
            myDrawing.Color = Colors.White;
            myDrawing.Width = myDrawing.Height = strokeWeight;
        }

        private void inkPrt_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.inkPrt.CaptureMouse();
            //获取点
            StylusPointCollection stPoint = e.StylusDevice.GetStylusPoints(this.inkPrt);
            MyStroke = new Stroke(stPoint);
            if (MyStroke!=null)
            {
                MyStroke.DrawingAttributes = myDrawing;
            }
            //添加到集合中
            this.inkPrt.Strokes.Add(MyStroke);

        }

        private void inkPrt_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (MyStroke!=null)
            {
                MyStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(this.inkPrt));
            }
        }

        private void inkPrt_LostMouseCapture(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //松开的时候 释放
            MyStroke = null;
        }
        //这个就是RadionButton  去获取颜色的方法。然后将取到的颜色赋值给画笔
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = e.OriginalSource as RadioButton;

            Rectangle rec = rb.Content as Rectangle;

            if (rec!=null)
            {
                myDrawing = new DrawingAttributes();
                //这个就是获取RadioButton 的Content 属性中的Color
                myDrawing.Color = ((SolidColorBrush)rec.Fill).Color;
                myDrawing.Width = myDrawing.Height = strokeWeight;
            }
        }
    }

如图


差不多就是这个效果了。

源码下载地址:http://download.csdn.net/detail/gongkepop/6246413

(写的不好请见谅,有不对请留言告知我,免得误人子弟。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值