0

I have a ttf-file. How do I draw a "J" using the ttf-files font and put it into a picturebox of 100 pixels height, cutting it out, so the top of the J hits the top of the picturebox and the bottom of the J hits the bottom of the picturebox? So the J is exactly 100 pixels tall, see this:

J

So in summary:

  1. Draw a "J" using the ttf-files font
  2. Cut it out, so from top to bottom of the J it is 100 pixels.
  3. Put it into a picturebox of 100 pixels height

This is my approach, which works but is not performant at all:

    void gv()
    {
        Bitmap bitmap = new Bitmap(1000,1000);


            Rectangle re = new Rectangle();
        using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
        {
            graphics.Clear(Color.White);
            Font font1 = new Font("Arial", 600, FontStyle.Regular, GraphicsUnit.Pixel);
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
            graphics.DrawString(was, font1, Brushes.Black, 0, 0);
            Color j = Color.FromArgb(0,0,0);
            for (int y = 1; y < 999; y++)
            {
                for (int x = 1; x < 999; x++)
                {

                    j = bitmap.GetPixel(x, y);
                    if (j.R == 0)
                    {
                        //MessageBox.Show("Oben y: "+y);
                        re.Y = y;
                        break;
                    }
                }
                if (j.R == 0)
                    break;
            }
            for (int y = 999; y > 1; y--)
            {
                for (int x = 999; x > 1; x--)
                {

                    j = bitmap.GetPixel(x, y);
                    if (j.R == 0)
                    {
                        //MessageBox.Show("Unten y: " + y);
                        re.Height = y - re.Y;
                        break;
                    }
                }
                if (j.R == 0)
                    break;
            }
            for (int x = 1; x < 999; x++)
            {
                for (int y = 1; y < 999; y++)
                {

                    j = bitmap.GetPixel(x, y);
                    if (j.R == 0)
                    {
                        //MessageBox.Show("Links x: " + x);
                        re.X = x;
                        break;
                    }
                }
                if (j.R == 0)
                    break;
            }
            for (int x = 999; x > 1; x--)
            {
                for (int y = 999; y > 1; y--)
                {

                    j = bitmap.GetPixel(x, y);
                    if (j.R == 0)
                    {
                        //MessageBox.Show("Rechts x: " + x);
                        re.Width = x - re.X;
                        break;
                    }
                }
                if (j.R == 0)
                    break;
            }
            //graphics.DrawRectangle(new Pen(Brushes.Black), re);
        }

        pictureBox1.Image = ResizeImage(CropBitmap(bitmap, re.X, re.Y, re.Width, re.Height),85,100);
    }

    public static Bitmap ResizeImage(Image image, int width, int height)
    {
        var destRect = new Rectangle(0, 0, width, height);
        var destImage = new Bitmap(width, height);

        destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        using (var graphics = Graphics.FromImage(destImage))
        {
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            using (var wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }

        return destImage;
    }

    public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight)
    {
        Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
        Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
        return cropped;
    }
Vitalis Hommel
  • 990
  • 2
  • 8
  • 20
  • If you are stuck on pixel perfect placement, try [this](http://stackoverflow.com/questions/33148543/how-to-draw-a-string-at-a-pixel-perfection-position/33152250#33152250) – TaW Oct 28 '15 at 13:56
  • Yes, `GetPixel` is rather slow. Either use `LockBits` (or `unsafe`) for ~10x faster access to the Pixels, or try to optimize the loops by starting not a 0 etc but around the known bounding box limits. The link I gave you will show you how to get the bounding box by using a `GraphicsPath`? ! – TaW Oct 28 '15 at 15:56
  • @TaW I tried your code. My solution ( with lockbits now ) cuts out the image, your red bounding box is never around the object but shifted to the left. How do I make it be around the letter so I can use the coordinates to cut it? – Vitalis Hommel Oct 29 '15 at 00:03
  • 'Yes, the red box is __showing__ the bounding box including all the white space for demonstration. I __use__ it to __move__ the letter I then draw.. – TaW Oct 29 '15 at 01:40

0 Answers0