简体   繁体   中英

C# add avatar to RichTextBox

What I'm trying to accomplish here , is having the ability to add a text msg like this:

在此处输入图片说明

to be able to make a chat box , where I have lets say 1000 msgs , like this .. So I created a custom RichTextBox as follow :

public class CustomRichTextBox : RichTextBox
    {
        public CustomRichTextBox()
        {

        }
        public static byte[] ImageToByte(Image img)
        {
            ImageConverter converter = new ImageConverter();
            return (byte[])converter.ConvertTo(img, typeof(byte[]));
        }

        public static string ByteArrayToString(byte[] ba)
        {
            StringBuilder hex = new StringBuilder(ba.Length * 2);
            foreach (byte b in ba)
                hex.AppendFormat("{0:x2}", b);
            return hex.ToString();
        }

        public void AddPic(Image img)
        {
            string imgStr = ByteArrayToString(ImageToByte(img));
            string mpic = @"{\pict\pngblip\picw" +
                          img.Width.ToString() + @"\pich" + img.Height.ToString() +
                          @"\picwgoal" + img.Width.ToString() + @"\pichgoal" + img.Height.ToString() +
                          @"\hex " + imgStr + "}";

            Rtf +=  mpic + " Username \r\n Said!...";
        }
    }

but nothing really shows on the RichTextBox , what I'm missing here!? I just want it to show the avatar and a paragraph next to it , later I will change the font/color etc

I think your avatar image as well as textbox for username should be separate controls from comment/message contents. This is sort of how it should be done:

<StackPanel orientation="horizontal">
    <Image alt="avatar" />
    <StackPanel orientation="vertical">
        <TextBox>Username</TextBox>
        <RichTextBox>Message or Comment</RichTextBox>
    </StackPanel>
</StackPanel>

Translate to your layout of choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM