简体   繁体   中英

c# RichTextbox Paste method

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.

由于您实际上是在创建自己的RTF格式,并将其附加到控件中,因此我认为您可以使用类似以下内容的方法:

RTB.Rtf += mpic;

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