2012-09-11 80 views
0

RichTextbox粘貼方法究竟如何工作?我想插入圖片到RTB。如果我使用Clipboard.SetImage(圖片);然後RTB.Paste();它工作正常。但是我想用我自己來做。我有以下代碼。但它不起作用:c#RichTextbox粘貼方法

 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; 

我發現在使用Paste()方法後,hexString中的differend值比richTextbox中的differend值更大。

這工作正常,如果我使用正確的六進制值。

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

我想我只需要將圖像轉換爲WMF六進制值。但我不知道該怎麼做。

這是什麼工作,如果有幫助:

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

感謝響應。

+0

看起來有前得到答覆:HTTP:// stackoverflow.com/questions/542850/how-can-i-insert-an-image-into-a-richtextbox – PaulPerry

回答

0

因爲你實際上是創建自己的RTF格式,並將其附加到控制,我想你可以使用類似:

RTB.Rtf += mpic; 
+0

感謝您的回覆。我已經使用這個解決方案來獲得我想要的hexa值:http://www.codeproject.com/Articles/4544/Insert-Plain-Text-and-Images-into-RichTextBox-at-R它的工作原理是: ) – sczdavos

+0

優秀。很高興聽到你有一個解決方案。 –