.Net6 图片转文字PictureToTxt

一、创建工程

创建工程后,打开Nuget

输入Spire.OCR,进行安装

二、代码编写

using Spire.OCR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PictureToTxt
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Convert_Click(object sender, RoutedEventArgs e)
        {
            string filePath = FilePath.Text;
            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    OcrScanner scanner = new OcrScanner();
                    if (scanner.Scan(filePath)==true)
                    {
                        ConvertText.Text = scanner.Text.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Grid_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Link;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void Grid_Drop(object sender, DragEventArgs e)
        {
            try
            {
                var fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                FilePath.Text = fileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TextBox_PreviewDrop(object sender, DragEventArgs e)
        {
            try
            {
                ((TextBox)sender).Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TextBox_PreviewDragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Link;
                e.Handled = true;
            }
            else
            { 
                e.Effects = DragDropEffects.None;
            }
        }

    }
}

界面

<Window x:Class="PictureToTxt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PictureToTxt"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" AllowDrop="True" Drop="Grid_Drop" DragEnter="Grid_DragEnter">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="17*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="40*"/>
        </Grid.RowDefinitions>
        <Grid Margin="10,10,10,10" Grid.Row="0" Grid.ColumnSpan="2">
            <GroupBox Header="将文件拖入此位置"></GroupBox>
            <Label Content="图片URL:" HorizontalAlignment="Left" Margin="45,0,0,0" VerticalAlignment="Center"/>
            <TextBox x:Name="FilePath" AllowDrop="True" HorizontalAlignment="Left" Margin="115,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Center" Width="405"  Drop="Grid_Drop" DragEnter="Grid_DragEnter"/>
            <Button x:Name="Convert"  Content="转换" HorizontalAlignment="Left" Margin="580,0,0,0" VerticalAlignment="Center" Click="Convert_Click"/>
        </Grid>
        <TextBox x:Name="ConvertText"  Grid.Row="1" TextWrapping="Wrap" Text="" Grid.ColumnSpan="2" />
    </Grid>
</Window>

修改工程为x64

复制动态链接库,这一点很重要

找到Spire.OCR包的位置,找到运行库的依赖,复制到程序根目录

复制后

运行程序(不能用调试模式),拖动图片到GroupBox中,然后点击转换

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花开花落的个人博客

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值