3

I'm trying to get text to wrap around an image, the closest I've gotten so far is to place both the image in a FlowLayoutPanel and use this code to create labels to fit allow my text to fit:

    private void Rules_Load(object sender, EventArgs e)
    {
        string RuleText = "some long string";
        string[] RuleWords = RuleText.Split(' ');
        Label[] RulesLabelArray = new Label[RuleWords.GetLength(0)];
        for (int i = 0; i < RuleWords.GetLength(0); i++)
        {
            RulesLabelArray[i] = new Label();
            flpRules.Controls.Add(RulesLabelArray[i]);
            RulesLabelArray[i].Text = RuleWords[i];
            RulesLabelArray[i].AutoSize = true;

        }
    }

however this technique creates them such that there are large spaces between words and only the first line next to the image has text on but not the next so it looks like this:

XXX ~ ~ ~ ~ ~ ~ ~

XXX

XXX

~ ~ ~ ~ ~ ~ ~

Where X represents the image and ~ represents text.

Why is this not working as expected and is there any other solution to this problem?

EDIT: as Sinatr said in comments, the outcome I want for this is for the text to be on the right of the image and continue below it if there is too much text so that it would appear like this (using same key as above):

XXX ~~~~~~~~~

XXX ~~~~~~~~~

XXX ~~~~~~~~~

~~~~~~~~~~~~~

~~~~~~~~~~~~~

Andrew Neate
  • 109
  • 1
  • 9
  • 1
    You forgot to add how you want it to be. I am guessing, you want the text of label to occupy place on the right of image AND continue below image if there is too much of text? Why do you want to do it manually using controls btw? E.g. `RichTextBox` supports images and text wrappings. – Sinatr Nov 13 '17 at 10:01
  • "Wrap Around" means that you are trying to insert a transparent .png file and flow text in the transparent areas? Or insert the text characters around borders of the image? – Naveed Abbas Nov 13 '17 at 10:09
  • @Sinatr could this RichTextBox be potentially used to achieve the same visual effect as what I'm trying to achieve (and yeah you're right I've edited question thanks) – Andrew Neate Nov 13 '17 at 10:20
  • @ToughGuy I think both of the methods you mentioned would give the same visual effect, so both. – Andrew Neate Nov 13 '17 at 10:24
  • With borders on the image, I was thinking to insert the text around image boundary only. The transparent image however requires you to read the transparent pixels data. – Naveed Abbas Nov 13 '17 at 10:26
  • These existing answers might help you. https://stackoverflow.com/questions/6809442/how-to-load-transparent-png-to-bitmap-and-ignore-alpha-channel https://stackoverflow.com/questions/3064854/determine-if-alpha-channel-is-used-in-an-image – Naveed Abbas Nov 13 '17 at 10:28
  • @ToughGuy ah I see sorry yeah it should be the second, the image won't have any unwanted whitespace – Andrew Neate Nov 13 '17 at 10:29
  • It's [quite easy](https://stackoverflow.com/a/3340956/1997232) to achieve in WPF. However in winforms it's a problem. I didn't find anything what achieve same with `RichTextBox` (and I am not rft expert to dig solution by my own). The "easiest" probably to split text in 2 portions: "right" text and "under" text. Then layout would be `TableLayoutPanel` with 2 rows and 2 columns, image goes in [0,0], right text in [1,0] and under text in [1,0] with columnspan. – Sinatr Nov 13 '17 at 10:48

0 Answers0