public Bitmap Draw(IEnumerable <LineData> lines) { foreach (var line in lines) { //create bitmap for line using (var lineImage = new Bitmap(line.TextSize.Width, line.TextSize.Height)) { var lineGraphics = Graphics.FromImage(lineImage); if (settings.TextAlign != Constants.TextAlignmentOptions.Justify) { PInvoked.TextOut(lineGraphics, line.Text, 0, 0, font, settings.LetterSpacing); } else { Justify(line, lineGraphics); } //draw line bitmap to image graphics.DrawImage(lineImage, new Rectangle( line.LineBounds.Location, lineImage.Size ), /* destination rect */ 0, 0, /* source coordinates */ lineImage.Width, lineImage.Height, GraphicsUnit.Pixel, imageAttributes ); #if DEBUG //draw rectangles graphics.DrawRectangle(Pens.White, line.LineBounds); graphics.DrawLine(Pens.Gray, line.LineBounds.X, line.LineBounds.Y + line.TextSize.Height / 2, line.LineBounds.X + line.TextSize.Width, line.LineBounds.Y + line.TextSize.Height / 2 ); #endif lineGraphics.Dispose(); } } //create selection-sized bitmap var resultImage = new Bitmap(selectionBounds.Width, selectionBounds.Height); var resultGraphics = Graphics.FromImage(resultImage); resultGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; resultGraphics.DrawImage(image, 0, 0, selectionBounds.Width, selectionBounds.Height); return(resultImage); }
private void Justify(LineData line, Graphics lineGraphics) { var lineTextWithoutSpaces = line.Text.Replace(Constants.Space, string.Empty); var lineSizeWithoutSpaces = PInvoked.MeasureString(lineGraphics, lineTextWithoutSpaces, font, settings.LetterSpacing); var spaceWidth = (line.TextSize.Width - lineSizeWithoutSpaces.Width) / Math.Max((line.Text.Length - lineTextWithoutSpaces.Length), 1); if (spaceWidth > font.Size * 3) { PInvoked.TextOut(lineGraphics, line.Text, 0, 0, font, settings.LetterSpacing); } else { var x = 0; foreach (string word in line.Text.Split(Constants.SpaceChar)) { var wordSize = PInvoked.MeasureString(lineGraphics, word, font, settings.LetterSpacing); PInvoked.TextOut(lineGraphics, word, x, 0, font, settings.LetterSpacing); x += wordSize.Width + spaceWidth; } } }