wpf中ScrollViewer中的Canvas中的图片怎么样实现拖拽?详解

 我来答
熊_
2012-03-22 · TA获得超过114个赞
知道答主
回答量:106
采纳率:0%
帮助的人:65.3万
展开全部
看看这个案例有没有帮助

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;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式