wpf中ScrollViewer中的Canvas中的图片怎么样实现拖拽?详解
1个回答
展开全部
看看这个案例有没有帮助
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;
}
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询