0

How exactly works RichTextbox Paste method? I'd like insert images to RTB. If I use Clipboard.SetImage(image); and then RTB.Paste(); it works fine. But I'd like make it by my self. I've following code. But it doesn't work:

        byte[] byteArray = null;
        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, ImageFormat.Png);
            ms.Seek(0, 0);
            byteArray = ms.ToArray();
        }
        string hexString = "";
        foreach (byte b in byteArray)
        {
            hexString += b.ToString("x2");
        }

        string mpic = @"{\pict\pngblip\picw396\pich396\picwgoal225\pichgoal225 ";
        mpic += hexString;
        mpic += "}";

        Console.WriteLine(mpic);
        return mpic;

I found that in hexString is differend value than in richTextbox after use Paste() method.

This works fine if I use correct hexa value.

        string mpic = @"{\pict\wmetafile8\picw396\pich396\picwgoal225\pichgoal225 ";
        mpic += hexString;
        mpic += "}";

I think what I need just convert image to WMF hexa value. But I don't know how to do that.

This is what works if it helps:

010009000003a001000000008a0100000000050000000b0200000000050000000c028c018c018a
010000430f2000cc0000000f000f00000000008c018c0100000000280000000f0000000f000000
0100180000000000d0020000c40e0000c40e00000000000000000000ffffffffffffffffffc5d9
d83a78750a4442011f1e000404011f1e0a44423a7875c5d9d8ffffffffffffffffff000000ffff
ffffffff669896073e3c00636000a7a100dcd400fbf200dcd400a7a1006360073e3c669896ffff
ffffffff000000ffffff66989604393700b0aa00fff600fff600fff600fff600fff600fff600ff
f600b0aa043937669896ffffff000000c5d9d8073e3c00b0aa00fff600fff600fff600fff600ff
f600fff600fff600fff600fff600b0aa073e3cc5d9d80000003a787500636000fff600fff600ff
f600000000000000000000000000000000fff600fff600fff60063603a78750000000a444200a7
a100fff600fff600000000fff600fff600fff600fff600fff600000000fff600fff600a7a10a44
42000000011f1e00dcd400fff600000000fff600fff600fff600fff600fff600fff600fff60000
0000fff600dcd4011f1e00000000040400fbf200fff600fff600fff600fff600fff600fff600ff
f600fff600fff600fff600fff600fbf2000404000000011f1e00dcd400fff600fff600fff600ff
f600fff600fff600fff600fff600fff600fff600fff600dcd4011f1e0000000a444200a7a100ff
f600fff600000000fff600fff600fff600fff600fff600000000fff600fff600a7a10a44420000
003a787500636000fff600fff600000000fff600fff600fff600fff600fff600000000fff600ff
f60063603a7875000000c5d9d8073e3c00b0aa00fff600fff600fff600fff600fff600fff600ff
f600fff600fff600b0aa073e3cc5d9d8000000ffffff66989604393700b0aa00fff600fff600ff
f600fff600fff600fff600fff600b0aa043937669896ffffff000000ffffffffffff669896073e
3c00636000a7a100dcd400fbf200dcd400a7a1006360073e3c669896ffffffffffff000000ffff
ffffffffffffffc5d9d83a78750a4442011f1e000404011f1e0a44423a7875c5d9d8ffffffffff
ffffffff000000030000000000

Thanks for responds.

sczdavos
  • 2,035
  • 11
  • 37
  • 71
  • Looks like it has been answered before: http://stackoverflow.com/questions/542850/how-can-i-insert-an-image-into-a-richtextbox – PaulPerry Sep 11 '12 at 16:23

1 Answers1

0

Since you're actually creating your own RTF formatting, and appending it to the control, I'd think you could use something like:

RTB.Rtf += mpic;
Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
  • Thanks for respond. I've already used this solution to get hexa value I want: http://www.codeproject.com/Articles/4544/Insert-Plain-Text-and-Images-into-RichTextBox-at-R And it works perfectly :) – sczdavos Sep 11 '12 at 16:18
  • Excellent. Glad to hear you've got a solution. – Lynn Crumbling Sep 11 '12 at 16:26