将TTF文字转换为定义高度的图片框

时间:2015-10-28 12:56:06

标签: c# fonts

我有一个ttf文件。我如何画一个" J"使用ttf文件字体并将其放入100像素高度的图片框中,将其剪切掉,这样J的顶部会碰到图片框的顶部而J的底部会碰到图片框的底部?所以J正好是100像素高,请看:

J

总结如下:

  1. 画一个" J"使用ttf-files字体
  2. 剪掉它,所以从J的顶部到底部是100像素。
  3. 将其放入100像素高的图片框
  4. 这是我的方法,它起作用但根本不具备效果:

        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;
        }
    

0 个答案:

没有答案