Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapped text should not include space at start of new line #375

Closed
3 of 4 tasks
CDDelta opened this issue Oct 26, 2017 · 11 comments
Closed
3 of 4 tasks

Wrapped text should not include space at start of new line #375

CDDelta opened this issue Oct 26, 2017 · 11 comments

Comments

@CDDelta
Copy link

CDDelta commented Oct 26, 2017

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

When wrapping text with spaces to a new line DrawText() starts the next line with a space.

Steps to Reproduce

  1. Draw text (eg. "TEST TEST TEST TEST") that includes spaces and enable wrapping.
  2. Output and view it.

Here is an example I made.
output_512x512

System Configuration

  • ImageSharp version:
  • Other ImageSharp packages and versions: SixLabors.ImageSharp.Drawing 1.0.0-beta0001
  • Environment (Operating system, version and so on):
  • .NET Framework version: .NET Core 2.0
  • Additional information:
@tocsoft
Copy link
Member

tocsoft commented Oct 26, 2017

moved to SixLabors/Fonts#47 as this is a bug/issue with the layout engine built into SixLabors.Fonts

@tocsoft tocsoft closed this as completed Oct 26, 2017
@tocsoft
Copy link
Member

tocsoft commented Oct 26, 2017

thanks for reporting this, I've 'moved' this over to the other repository as its specifically an issue in that component.

@CDDelta
Copy link
Author

CDDelta commented Oct 26, 2017

Thanks for the swift reply!

@naveedahmed1
Copy link

@tocsoft can you please guide where can I find documentation of this plugin. I want to add some text on an image, but couldn't find any documentation or tutorial.

@tocsoft
Copy link
Member

tocsoft commented Oct 29, 2017

@naveedahmed1 as linked from our readme our samples repository has 2 different examples of drawing text on images. The Draw watermark on image and Draw text along a path both do this.

@naveedahmed1
Copy link

Thank you so much @tocsoft :)

@naveedahmed1
Copy link

I am using the latest version but its not recognizing SixLabors.Fonts

@tocsoft
Copy link
Member

tocsoft commented Oct 30, 2017

@naveedahmed1 make sure you have a dependency on SixLabors.ImageSharp.Drawing this is the package that enables drawing onto images.

@naveedahmed1
Copy link

@tocsoft can you please guide whats wrong with below code:


public async Task<FileResult> Test()
        {
            using (var img = Image.Load("fb.jpg"))
            {
                using (System.IO.Stream outputImageStream = new MemoryStream())
                {
                    Font font = SystemFonts.CreateFont("Arial", 10); // for scaling water mark size is largly ignored.

                    string text = "Hello world!";
                    float padding = 5;

                    img.Mutate(ctx => ctx.Apply(image =>
                     {
                         float targetWidth = image.Width - (padding * 2);
                         float targetHeight = image.Height - (padding * 2);

                         // measure the text size
                         SizeF size = TextMeasurer.Measure(text, new RendererOptions(font));

                         //find out how much we need to scale the text to fill the space (up or down)
                         float scalingFactor = Math.Min(image.Width / size.Width, image.Height / size.Height);

                         //create a new font 
                         Font scaledFont = new Font(font, scalingFactor * font.Size);

                         var center = new PointF(image.Width / 2, image.Height / 2);

                         image.Mutate(i => i.DrawText(text, scaledFont, Rgba32.HotPink, center, new TextGraphicsOptions(true)
                         {
                             HorizontalAlignment = HorizontalAlignment.Center,
                             VerticalAlignment = VerticalAlignment.Center
                         }));


                         JpegEncoder encoder = new JpegEncoder()
                         {
                             Quality = 100
                         };

                         img.SaveAsJpeg(outputImageStream, encoder);

                     }));

                    outputImageStream.Position = 0;
                    return new FileStreamResult(outputImageStream, "image/png");
                }

            }
        }

I am receiving below error:

An unhandled exception occurred while processing the request.

ObjectDisposedException: Cannot access a closed Stream.
System.IO.__Error.StreamIsClosed()

@dlemstra
Copy link
Member

You should not Dispose the outputImageStream passed to the FileStreamResult object. That class becomes the owner and will release it themselves.

@naveedahmed1
Copy link

Thank you so much @dlemstra it was really helpful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants