草庐IT

c# - 如何在 wpf c# 中的 WritableBitmap 中选择或突出显示鼠标移动事件中的文本

coder 2024-05-31 原文

我有 WritableBitmap 图像,我已经在图像控件 src 中设置了。当用户在选定的文本区域上移动时,我正在创建矩形。我还使用 PDFtron SDK 从 PDF 文档中获取选定的文本。 我们从 PDF 中获取 WritableBitmap 图像。我们必须在线选择文本。

我正在使用这段代码绘制屏幕:

System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)Math.Min(_downX, x),
                             (int)Math.Min(_downY, y),
                             (int)Math.Abs(_downX - x),
                             (int)Math.Abs(_downY - y));

System.Drawing.Bitmap myBitmap = new System.Drawing.Bitmap(@"D:\PDF\ScreenDraw\WpfApplication1\WpfApplication1\Image\Capture.PNG");

using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myBitmap))
{
    System.Drawing.Color customColor = System.Drawing.Color.FromArgb(50, System.Drawing.Color.Red);
    System.Drawing.SolidBrush shadowBrush = new System.Drawing.SolidBrush(customColor);
    g.FillRectangles(shadowBrush, new System.Drawing.Rectangle[] { rectangle });
}

//myBitmap.Save(@"D:\PDF\abc.png");
//bitmapSource = new BitmapImage(new Uri(@"D:\PDF\abc.png", UriKind.Absolute));

using (var memory = new System.IO.MemoryStream())
{
    myBitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
    memory.Position = 0;

    var bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = memory;
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.EndInit();
    Img.Source = bitmapImage;
}

我如何选择线而不是 Rect 的文本?

我必须选择如上图所示的文本。

最佳答案

你想要的是不可能的。你有一个位图,它并没有神奇地意识到其中的文本,并且没有任何改变。 虽然并不是说你无能为力。 我没有时间提供完整的解决方案,但我可以提供有关如何实现最佳解决方案的分步说明。

你可以做的是:

  1. 文本尺寸定义 - 创建带有覆盖在图像上的网格的控件,具有可编辑的 X 轴和 Y 轴步长和偏移量。然后您将能够使用文本行 (Y) 校准网格。和字符宽度 (X)。 应该做这样的事情(我想你会明白一般的想法):

    public int XGridStep
    {
        get { return (int)base.GetValue(XGridStepProperty); }
        set
        {
            base.SetValue(XGridStepProperty, value);
            RepaintGrid();
        }
    }
    
    public static readonly DependencyProperty XGridStepProperty = DependencyProperty.Register("XGridStepProperty", typeof(int), typeof(PlanLayout), new PropertyMetadata(100));
    
    public int XGridOffset
    {
        get { return (int)base.GetValue(XGridOffsetProperty); }
        set
        {
            base.SetValue(XGridOffsetProperty, value);
            RepaintGrid();
        }
    }
    
    public static readonly DependencyProperty XGridOffsetProperty = DependencyProperty.Register("XGridOffsetProperty", typeof(int), typeof(PlanLayout), new PropertyMetadata(0));
    
    public bool XGridVisible
    {
        get { return (bool)base.GetValue(XGridVisibleProperty); }
        set
        {
            base.SetValue(XGridVisibleProperty, value);
            RepaintGrid();
        }
    }
    
    public static readonly DependencyProperty XGridVisibleProperty = DependencyProperty.Register("XGridVisibleProperty", typeof(bool), typeof(PlanLayout), new PropertyMetadata(false));
    
    public int YGridStep
    {
        get { return (int)base.GetValue(YGridStepProperty); }
        set
        {
            base.SetValue(YGridStepProperty, value);
            RepaintGrid();
        }
    }
    
    public static readonly DependencyProperty YGridStepProperty = DependencyProperty.Register("YGridStepProperty", typeof(int), typeof(PlanLayout), new PropertyMetadata(100));
    
    public int YGridOffset
    {
        get { return (int)base.GetValue(YGridOffsetProperty); }
        set
        {
            base.SetValue(YGridOffsetProperty, value);
            RepaintGrid();
        }
    }
    
    public static readonly DependencyProperty YGridOffsetProperty = DependencyProperty.Register("YGridOffsetProperty", typeof(int), typeof(PlanLayout), new PropertyMetadata(0));
    
    public bool YGridVisible
    {
        get { return (bool)base.GetValue(YGridVisibleProperty); }
        set
        {
            base.SetValue(YGridVisibleProperty, value);
            RepaintGrid();
        }
    }
    
    public static readonly DependencyProperty YGridVisibleProperty = DependencyProperty.Register("YGridVisibleProperty", typeof(bool), typeof(PlanLayout), new PropertyMetadata(false));
    
    private void RepaintGrid()
    {
        if (!IsEditable)
            return;
    
        foreach (Line l in _gridXLines)
            content.Children.Remove(l);
        _gridXLines.Clear();
        if (XGridVisible)
            for (int i = XGridOffset; i < content.ActualWidth; i += XGridStep)
            {
                Line line = new Line();
                line.IsHitTestVisible = false;
                line.Stroke = Brushes.Black;
                line.Y1 = 0;
                line.Y2 = content.ActualHeight;
                line.X1 = line.X2 = i;
                if (Math.Abs(line.X1 - content.ActualWidth) < XGridStep * 0.5 || line.X1 < XGridStep * 0.5)
                    continue;
                _gridXLines.Add(line);
                content.Children.Add(line);
                Canvas.SetZIndex(line, 0);
            }
    
        foreach (Line l in _gridYLines)
            content.Children.Remove(l);
        _gridYLines.Clear();
        if (YGridVisible)
            for (int i = YGridOffset; i < content.ActualHeight; i += YGridStep)
            {
                Line line = new Line();
                line.IsHitTestVisible = false;
                line.Stroke = Brushes.Black;
                line.X1 = 0;
                line.X2 = content.ActualWidth;
                line.Y1 = line.Y2 = i;
                if (Math.Abs(line.Y1 - content.ActualHeight) < YGridStep * 0.5 || line.Y1 < YGridStep * 0.5)
                    continue;
                _gridYLines.Add(line);
                content.Children.Add(line);
                Canvas.SetZIndex(line, 0);
            }
    }
    
  2. 文本选择 - 您现在要做的就是向您的控件添加“对齐网格”功能。同样,仅供引用:

    private void elementWrapper_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (_mouseHandlingMode != MouseHandlingMode.Dragging)
            return;
    
        SelectableElement element = (SelectableElement)sender;
    
        Point curContentPoint = e.GetPosition(content);
        //Vector elementDragVector = curContentPoint - _origContentMouseDownPoint;
    
        _origContentMouseDownPoint = curContentPoint;
    
        //double destinationLeft = Canvas.GetLeft(element) + elementDragVector.X;
        //double destinationTop = Canvas.GetTop(element) + elementDragVector.Y;
        double destinationLeft = curContentPoint.X - element.ActualWidth / 2;
        double destinationTop = curContentPoint.Y - element.ActualHeight / 2;
    
        if (SnapToGrid)
        {
            if (XGridVisible)
            {
                foreach (Line l in _gridXLines)
                    l.StrokeThickness = 1;
    
                Line nearest = GetNearestXGridLine((int)curContentPoint.X);
    
                if (Math.Abs(curContentPoint.X - nearest.X1) < XGridStep * 0.2)
                {
                    destinationLeft = nearest.X1 - element.ActualWidth / 2;
                    nearest.StrokeThickness = 3;
                }
            }
    
            if (YGridVisible)
            {
                foreach (Line l in _gridYLines)
                    l.StrokeThickness = 1;
    
                Line nearest = GetNearestYGridLine((int)curContentPoint.Y);
    
                if (Math.Abs(curContentPoint.Y - nearest.Y1) < YGridStep * 0.2)
                {
                    destinationTop = nearest.Y1 - element.ActualHeight / 2;
                    nearest.StrokeThickness = 3;
                }
            }
        }
    
        if (destinationLeft < 0)
            destinationLeft = 0;
    
        if (destinationLeft > content.ActualWidth - element.ActualWidth)
            destinationLeft = content.ActualWidth - element.ActualWidth;
    
        if (destinationTop < 0)
            destinationTop = 0;
    
        if (destinationTop > content.ActualHeight - element.ActualHeight)
            destinationTop = content.ActualHeight - element.ActualHeight;
    
        Canvas.SetLeft(element, destinationLeft);
        Canvas.SetTop(element, destinationTop);
    
        element.ElementContent.Position.X = curContentPoint.X;
        element.ElementContent.Position.Y = curContentPoint.Y;
    
        e.Handled = true;
    }
    
    private Line GetNearestXGridLine(int xpos)
    {
        return _gridXLines.OrderBy(gl => Math.Abs((int)gl.X1 - xpos)).First();
    }
    
    private Line GetNearestYGridLine(int Ypos)
    {
        return _gridYLines.OrderBy(gl => Math.Abs((int)gl.Y1 - Ypos)).First();
    }
    
  3. 选择的图形表示 - 现在绘制(最多)3 个矩形:从选择的左上点到相关文本行的右下点,下一行的左上点到最后选择的前一行的右下点和最后一行的左上点到选择的右下角

  4. 获取文本 - 从这些矩形中获取部分文本数据并加入。

关于c# - 如何在 wpf c# 中的 WritableBitmap 中选择或突出显示鼠标移动事件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31579033/

有关c# - 如何在 wpf c# 中的 WritableBitmap 中选择或突出显示鼠标移动事件中的文本的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  4. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  5. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  6. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  7. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  8. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  9. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  10. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

随机推荐