c# - How can I insert an image into a RichTextBox? -


most of examples see put on clipboard , use paste, doesn't seem because overwrites clipboard.

i did see one method manually put image rtf using pinvoke convert image wmf. best way? there more straightforward thing can do?

the straightforward way modify rtf code insert picture yourself.

in rtf, picture defined this:

'{' \pict (brdr? & shading? & picttype & pictsize & metafileinfo?) data '}' question mark indicates control word optional. "data" content of file in hex format. if want use binary, use \bin control word.

for instance:

{\pict\pngblip\picw10449\pich3280\picwgoal5924\pichgoal1860 hex data} {\pict\pngblip\picw10449\pich3280\picwgoal5924\pichgoal1860\bin binary data} 

\pict = starts picture group, \pngblip = png picture \picwx = width of picture (x pixel value) \pichx = height of picture \picwgoalx = desired width of picture in twips

so, insert picture, need open picture, convert data hex, load these data string , add rtf codes around define rtf picture. now, have self contained string picture data can insert in rtf code of document. don't forget closing "}"

next, rtf code richtextbox (rtbbox.rtf), insert picture @ proper location, , set code of rtbbox.rtf

one issue may run .net rtb not have support of rtf standard.

i have made small application* allows test rtf code inside rtb , see how handles it. can download here: rtb tester (http://your-translations.com/toys).

you can paste rtf content (from word, instance) left rtf box , click on "show rtf codes" display rtf codes in right rtf box, or can paste rtf code in right rtb , click on "apply rtf codes" see results on left hand side.

you can of course edit codes like, makes quite convenient testing whether or not richtextbox supports commands need, or learn how use rtf control words.

you can download full specification rtf online.


nb it's little thing slapped in 5 minutes, didn't implement file open or save, drag , drop, or other civilized stuff.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -