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;
        }

你可能感兴趣的:(bitmap)