5

I am doing an OCR project. I must to get an image from input in inkcanvas to process in the next step, i.e. translate this image to an two-dimension array.

I am confused about that how to get a bitmap image from inkcanvas to process. I have been searching for a solution from many sources but a lot of them just save the inkcanvas to file-stream.

please help me! thanks so much

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
nguyen
  • 171
  • 6
  • 13

2 Answers2

2

I know this question is older but I also had to get a bitmap from the ink canvas. So to answer the question on how to get a bitmap directly from the ink canvas, here is a solution. hope it still helps.

    private System.Drawing.Image ConvertInkCanvasToImage()
    {

      //create temporary InkCanvas or send it in as a parameter
      InkCanvas inkCanvas = new InkCanvas();      
      inkCanvas = viewModel.InkCanvas;

      //render bitmap
      RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96, 96, System.Windows.Media.PixelFormats.Default);
      rtb.Render(inkCanvas);
      BmpBitmapEncoder encoder = new BmpBitmapEncoder();
      encoder.Frames.Add(BitmapFrame.Create(rtb));
      rtb.Render(inkCanvas);

      //save to memory stream or file 
      System.IO.MemoryStream ms = new System.IO.MemoryStream();
      encoder.Save(ms);  

      //creat bitmap with memory stream or file
      Bitmap bitmap = new Bitmap(ms);
      return bitmap;
    }
bbedson
  • 133
  • 1
  • 8
  • If you need to copy the image to the clipboard, then after you save it to memory stream, you have to create a bitmap from the memory stream and copy it to the clipboard, like this: `var img = Bitmap.FromStream(ms); Clipboard.SetDataObject(img);` – user8581607 Feb 13 '21 at 22:21
1

Check this blog post: http://www.centrolutions.com/Blog/post/2008/12/09/Convert-WPF-InkCanvas-to-Bitmap.aspx

That gets you a byte array for the bitmap, since you're doing OCR that should be enough.

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
Filipe Miguel
  • 519
  • 1
  • 6
  • 13