使用自定义的矩形图像裁切 [英] Image crop with custom rectangle

查看:149
本文介绍了使用自定义的矩形图像裁切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的应用程序的图像WIA扫描。但是,我的图片有很多未使用的空间,如果扫描文件的大小并不大。我需要裁剪的未使用空间像漆十字光标和矩形。如何做到这一点在WPF? 图像裁剪的code是:

 私有静态图片cropImage(图片IMG,矩形cropArea)
{
 位图bmpImage =新位图(IMG);
 位图bmpCrop = bmpImage.Clone(cropArea,bmpImage.PixelFormat);
 回报(图)(bmpCrop);
}
 

解决方案

把Image控件在Canvas,并添加一个不可见的矩形到画布上也是如此。

在鼠标左键设置矩形的左上角的鼠标坐标,并使其可见。

在鼠标移动(和鼠标按键还是向下),设置矩形的大小,所以在第一个弯道移动鼠标相反。

例如:

 <窗​​口x:类=TestWpfApplication.MainWindow
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        XMLNS:地方=CLR的命名空间:TestWpfApplication
        标题=主窗口
        高度=350
        宽度=525>
    <帆布的MouseLeftButtonDown =Canvas_MouseLeftButtonDown
            的MouseMove =Canvas_MouseMove
            背景=古董白>
        <图像宽度=500
               高度=300/>
        <矩形X:名称=selectionRectangle
                   StrokeThickness =1
                   行程=浅蓝色
                   填写=#220000FF
                   能见度=坍塌/>
    < /帆布>

< /窗>
 

和后面的code:

 使用System.Windows;
使用System.Windows.Controls的;
使用System.Windows.Input;

命名空间TestWpfApplication
{
    公共部分类主窗口:System.Windows.Window
    {
        公共主窗口()
        {
            的InitializeComponent();
        }

        私人无效Canvas_MouseLeftButtonDown(对象发件人,MouseButtonEventArgs E)
        {
            VAR mousePosition = e.GetPosition(发件人为UIElement的);
            Canvas.SetLeft(selectionRectangle,mousePosition.X);
            Canvas.SetTop(selectionRectangle,mousePosition.Y);
            selectionRectangle.Visibility = System.Windows.Visibility.Visible;
        }

        私人无效Canvas_MouseMove(对象发件人,发送MouseEventArgs E)
        {
            如果(e.LeftButton == MouseButtonState。pressed)
            {
                VAR mousePosition = e.GetPosition(发件人为UIElement的);
                selectionRectangle.Width = mousePosition.X  -  Canvas.GetLeft(selectionRectangle);
                selectionRectangle.Height = mousePosition.Y  -  Canvas.GetTop(selectionRectangle);
            }
        }
    }
}
 

请注意,在画布将只有当它有一个颜色的点击响应。

另外:你需要处理,用户拖动选择从右到左和/或底部到顶部,因为这将引入负大小的矩形的宽度和高度的情况。但我会留给你。

I'm creating app for image scan with WIA. But my images has a lot of unused space if scanned document size is not big. I need to crop that unused space like in Paint with Cross cursor and rectangle. How to do that in WPF? Code of image cropping is:

private static Image cropImage(Image img, Rectangle cropArea)
{
 Bitmap bmpImage = new Bitmap(img);
 Bitmap bmpCrop = bmpImage.Clone(cropArea,bmpImage.PixelFormat);
 return (Image)(bmpCrop);
}

解决方案

Put the Image control in a Canvas and add an invisible rectangle to the canvas as well.

On left mouse button down set the top left of the rectangle at the mouse coordinates and make it visible.

On mouse move (and mouse button still down), set the size of the rectangle, so the opposite of the first corner moves with the mouse.

Example:

<Window x:Class="TestWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWpfApplication"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Canvas MouseLeftButtonDown="Canvas_MouseLeftButtonDown"
            MouseMove="Canvas_MouseMove"
            Background="AntiqueWhite">
        <Image Width="500"
               Height="300" />
        <Rectangle x:Name="selectionRectangle"
                   StrokeThickness="1"
                   Stroke="LightBlue"
                   Fill="#220000FF"
                   Visibility="Collapsed" />
    </Canvas>

</Window>

And the code behind:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace TestWpfApplication
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var mousePosition = e.GetPosition(sender as UIElement);
            Canvas.SetLeft(selectionRectangle, mousePosition.X);
            Canvas.SetTop(selectionRectangle, mousePosition.Y);
            selectionRectangle.Visibility = System.Windows.Visibility.Visible;
        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var mousePosition = e.GetPosition(sender as UIElement);
                selectionRectangle.Width = mousePosition.X - Canvas.GetLeft(selectionRectangle);
                selectionRectangle.Height = mousePosition.Y - Canvas.GetTop(selectionRectangle);
            }
        }
    }
}

Note that the Canvas will only respond to a click when it has a color.

Also: you'll need to handle the situation where the user drags the selection right to left and/or bottom to top because that will introduce negative sizes for the width and height of the rectangle. But I'll leave that to you.

这篇关于使用自定义的矩形图像裁切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆