WriteableBitmap实现对图片的放大缩小


xaml:

<ScrollViewer Name="ScrollViewerName" Background="Transparent" Width="480" Height="480" VerticalScrollBarVisibility="Auto"                         VerticalContentAlignment="Center" HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Center">
                    <Image Name="SelectImage" Stretch="None"/></ScrollViewer>

cs:

   private void ChooserButton_Click(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask task = new PhotoChooserTask();
            task.Completed += new EventHandler<PhotoResult>(task_Completed);
            task.Show();
        }

        private int OriginWidth;
        private int OriginHeight;
        private Image OriginImage;
        void task_Completed(object sender, PhotoResult e)
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(e.ChosenPhoto);
            OriginImage = new Image();
            OriginImage.Source = bitmap;
            OriginWidth = bitmap.PixelWidth;
            OriginHeight = bitmap.PixelHeight;

            SelectImage.Source = bitmap;
            HeightTextbox.Text = bitmap.PixelHeight.ToString();
            WidthTextbox.Text = bitmap.PixelWidth.ToString();
        }

        private void ReSizeButton_Click(object sender, RoutedEventArgs e)
        {
            double scale_X = Convert.ToDouble(WidthTextbox.Text) / OriginWidth;
            //OriginWidth = Convert.ToInt32(WidthTextbox.Text);//保存本次图片的宽度的pix值
            double scale_Y = Convert.ToDouble(HeightTextbox.Text) / OriginHeight;
            //OriginHeight = Convert.ToInt32(HeightTextbox.Text);//保存本次图片的高度的pix值

            ScaleTransform Scale= SizeAdjust(scale_X, scale_Y);

            WriteableBitmap writebitmap = new WriteableBitmap(OriginImage, Scale);
            //riteableBitmap writebitmap = new WriteableBitmap(SelectImage, Scale);//根据保存上次的长宽值来进行伸缩图片
            writebitmap.Invalidate();
            SelectImage.Source = writebitmap;
        }

        private ScaleTransform SizeAdjust(double x,double y)
        {
            ScaleTransform scale=new ScaleTransform ();
            scale.CenterX=0;
            scale.CenterY=0;
            scale.ScaleX=x;
            scale.ScaleY=y;
            return scale;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值