缩放用户控件中的裁剪像素

我开发了一个用户控件。 用户控制就像一个放大镜玻璃。 用户控件具有图像按钮,其显示逐像素裁剪的图像。

StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/wallpaper.jpg", UriKind.RelativeOrAbsolute)); using (Windows.Storage.Streams.IRandomAccessStream fileStream = await storageFile.OpenAsync(FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight); fileStream.Seek(0); await writeableBitmap.SetSourceAsync(fileStream); writeableBitmap = writeableBitmap.Crop(Convert.ToInt32(xValue), Convert.ToInt32(yValue), 100, 100); MagnifyTip.image1.ImageSource = writeableBitmap; 

现在,MagnifyTip.image1具有一个设置为裁剪图像的图像源。 我的要求是缩放裁剪区域,然后将其指定给图像源。 用户控件看起来像这样 在此处输入图像描述 帮助将不胜感激

也许这对你有用,它和WPF允许的效率一样,因为代码中没有图像裁剪,它只是使用RenderTransform来做魔术。 运行下面的代码并将鼠标按在图像上,这样放大镜就像这样:

在此处输入图像描述

XAML:

                      

这就是背后的代码:

 using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media.Imaging; namespace WpfApplication1 { public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { ZoomFactor = 8; ImageZoomSize = 200; InitializeComponent(); BorderZoom.Visibility = Visibility.Hidden; } public double Xt { get; private set; } public double Yt { get; private set; } public double ZoomFactor { get; private set; } public int ImageZoomSize { get; private set; } public int ImageZoomSizeHalf { get { return ImageZoomSize/2; } } public Point CenterPoint { get { return new Point(ImageZoomSizeHalf, ImageZoomSizeHalf);} } private void FullImage_OnMouseDown(object sender, MouseButtonEventArgs e) { BorderZoom.Visibility = Visibility.Visible; FullImage_OnMouseMove(sender, e); } private void FullImage_OnMouseMove(object sender, MouseEventArgs e) { if (BorderZoom.Visibility == Visibility.Visible) { BorderZoom.Visibility = Visibility.Visible; var pos = e.GetPosition(FullImage); Canvas.SetLeft(BorderZoom, pos.X - ImageZoomSizeHalf); Canvas.SetTop(BorderZoom, pos.Y - ImageZoomSizeHalf); var isrc = FullImage.Source as BitmapSource; if(isrc == null) return; var h = (double)isrc.PixelHeight; var w = (double)isrc.PixelWidth; Xt = pos.X* (-ImageZoomSize/w) + ImageZoomSize/2.0; Yt = pos.Y * (-ImageZoomSize / h) + ImageZoomSize / 2.0; OnNotifyPropertyChanged("Xt"); OnNotifyPropertyChanged("Yt"); } } private void FullImage_OnMouseUp(object sender, MouseButtonEventArgs e) { BorderZoom.Visibility = Visibility.Hidden; } public event PropertyChangedEventHandler PropertyChanged; private void OnNotifyPropertyChanged(string propName) { if(PropertyChanged!= null) PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } } 

UPDATE

根据要求,请参阅下面的代码将放大器包装在用户控件中,如下所示:

嵌入用户控件

XAML for MagifiyingTipCtrl:

                    

MagifiyingTipCtrl的代码隐藏:

 using System.Windows.Media.Imaging; namespace WpfApplication1 { public partial class MagifiyingTipCtrl : UserControl { public MagifiyingTipCtrl() { ZoomFactor = 8; ZoomWidth = 136; ZoomHeight = 128; InitializeComponent(); } public static readonly DependencyProperty SourceImageProperty = DependencyProperty.Register("SourceImage", typeof (BitmapSource), typeof (MagifiyingTipCtrl)); public static readonly DependencyProperty XtProperty = DependencyProperty.Register("Xt", typeof(double), typeof(MagifiyingTipCtrl)); public static readonly DependencyProperty YtProperty = DependencyProperty.Register("Yt", typeof(double), typeof(MagifiyingTipCtrl)); public BitmapSource SourceImage { get { return (BitmapSource)GetValue(SourceImageProperty); } set { SetValue(SourceImageProperty, value); } } public double Xt { get { return (double)GetValue(XtProperty); } set { SetValue(XtProperty, value); } } public double Yt { get { return (double)GetValue(YtProperty); } set { SetValue(YtProperty, value); } } public void SetPosition(Point pos) { if (SourceImage == null) return; var h = (double)SourceImage.PixelHeight; var w = (double)SourceImage.PixelWidth; Xt = pos.X * (-ZoomWidth / w) + ZoomWidth / 2.0; Yt = pos.Y * (-ZoomHeight / h) + ZoomHeight / 2.0; } public double ZoomFactor { get; private set; } public int ZoomWidth { get; private set; } public int ZoomHeight { get; private set; } public int ZoomWidthHalf { get { return ZoomWidth / 2; } } public int ZoomHeightHalf { get { return ZoomHeight / 2; } } public Point CenterPoint { get { return new Point(ZoomWidthHalf, ZoomHeightHalf); } } } } 

MainWindow的XAML:

         

MainWindow的代码隐藏:

 using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void FullImage_OnMouseDown(object sender, MouseButtonEventArgs e) { MagnifiyingTip.Visibility = Visibility.Visible; FullImage_OnMouseMove(sender, e); } private void FullImage_OnMouseMove(object sender, MouseEventArgs e) { if (MagnifiyingTip.Visibility == Visibility.Visible) { MagnifiyingTip.Visibility = Visibility.Visible; var pos = e.GetPosition(FullImage); Canvas.SetLeft(MagnifiyingTip, pos.X - MagnifiyingTip.ActualWidth/2); Canvas.SetTop(MagnifiyingTip, pos.Y - MagnifiyingTip.ActualHeight); MagnifiyingTip.SetPosition(pos); } } private void FullImage_OnMouseUp(object sender, MouseButtonEventArgs e) { MagnifiyingTip.Visibility = Visibility.Hidden; } } } 

就像我在评论中写的那样,在你的PictureZoom的WPF中快速演示应用程序。

https://github.com/hrkrx/PictureZoomExample

它只是一个考试,所以可以有很多优化,但我希望它能帮助你