Nastya  Kononchuk
Nastya Kononchuk

Reputation: 23

Select only painted part of bitmap C#

I have an InkCanvas and I get from it an image and save this bitmap:

RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96d, 96d, PixelFormats.Default);
rtb.Render(inkCanvas);
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));

using (FileStream fs = File.Open(NeuralNetwork.Properties.Resources.DrawingFile, FileMode.Create))
{
    encoder.Save(fs);
}

And the program is targeting to receive a character. For instance, I've draw 'A' letter: 'A' letter on InkCanvas

And my problem is that I need to get an area (rectangle) where was written this letter. Brush is always black and background is always white. So I get this image: 'A' letter with cut edges

So I would be glad for your help to find an efficient algorithm to do it. Thanks on advance!

Upvotes: 0

Views: 60

Answers (1)

Nastya  Kononchuk
Nastya Kononchuk

Reputation: 23

Decided to close the issue and post method that finds topmost, rightmost, leftmost and bottommost points and how said Dmitry turned it into rectangle.

    /// <summary>
    /// Select an painted area of canvas by finding the top, the bottom, the right border and the left border of letter.
    /// </summary>
    /// <param name="p"></param>
    /// <returns>An croped image with no white borders.</returns>
    public static Bitmap CalculateRectangle(Bitmap p)
    {
        int TopMost = 0;
        int RightBorder = 0;
        int BotomMost = 0;
        int LeftBorder = 0;
        //maxH
        bool flag = false;
        for (int i = 0; i < p.Height; i++)
        {
            for (int j = 0; j < p.Width; j++)
            {
                if (!IsWhitePixel(p.GetPixel(j, i)))
                {
                    TopMost = i;
                    flag = true;
                    break;
                }
            }
            if (flag) break;
        }
        //minH
        flag = false;
        for (int i = p.Height - 1; i >= 0; i--)
        {
            for (int j = 0; j < p.Width; j++)
            {
                if (!IsWhitePixel(p.GetPixel(j, i)))
                {
                    BotomMost = i;
                    flag = true;
                    break;
                }
            }
            if (flag) break;
        }
        //left
        flag = false;
        for (int j = 0; j < p.Width; j++)
        {
            for (int i = TopMost; i <= BotomMost; i++)
            {
                if (!IsWhitePixel(p.GetPixel(j, i)))
                {
                    LeftBorder = j;
                    flag = true;
                    break;
                }
            }
            if (flag) break;
        }
        //right
        flag = false;
        for (int j = p.Width - 1; j >= 0; j--)
        {
            for (int i = TopMost; i <= BotomMost; i++)
            {
                if (!IsWhitePixel(p.GetPixel(j, i)))
                {
                    RightBorder = j;
                    flag = true;
                    break;
                }
            }
            if (flag) break;
        }
        return CropImage(p, new Rectangle(new System.Drawing.Point(LeftBorder, TopMost), new System.Drawing.Size(RightBorder - LeftBorder + 1, BotomMost - TopMost + 1)));
    }

Upvotes: 1

Related Questions