RichTextBox FlowDocument type operation,
A recent research project, with web protocol as a client, the overall WPF project MVVM architecture and basic code reference: http://www.cnblogs.com/ZXdeveloper/archive/2016/11/13/6058206.html
Because the RichTextBox binding image and background image processing are not implemented by referring to the project of the blogger, I have found some methods for some processing and recorded them to prevent future use, or someone may encounter this problem and provide some reference.
The specific value of RichTextBox is bound to the FlowDocument type. For details, refer to Microsoft's official introduction.
Obtain the image, text, and QQ expression in RichTextBox from the following body:
/// <Summary> /// convert the value in the Document into an image or text /// </summary> /// <param name = "summary"> </param >/// <returns> </returns> public void FlowDocumentMessage (FlowDocument workflow) {if (response! = Null) {string resutStr = string. empty; foreach (var root in logs. blocks) {if (root is BlockUIContainer) {System. windows. controls. image img = (System. windows. controls. image) (BlockUIContainer) root ). child; System. drawing. bitmap bitmap = GetBitmap (img);} else {foreach (var item in (Paragraph) root ). inlines) {// if it is Emoji, convert if (item is InlineUIContainer) {System. windows. controls. image img = (System. windows. controls. image) (InlineUIContainer) item ). child; resutStr + = GetEmojiName (img. source. toString ();} // if it is text, assign the value if (item is Run) {resutStr + = (Run) item ). text ;}}}}}}
It is difficult to convert System. Windows. Controls. Image to System. Drawing. Imgage.
/// <Summary> /// set System. windows. controls. the BitmapSource of the Image is converted to System. drawing. bitmap // </summary> /// <param name = "image"> </param> /// <returns> </returns> private System. drawing. bitmap GetBitmap (System. windows. controls. image image) {System. windows. media. imaging. bitmapSource transformedBitmapSource = image. source as BitmapSource; int width = transformedBitmapSource. pixelWidth; int height = transformedBitmapSource. pixelHeight; int stride = width * (transformedBitmapSource. format. bitsPerPixel + 7)/8); byte [] bits = new byte [height * stride]; transformedBitmapSource. copyPixels (bits, stride, 0); unsafe {fixed (byte * pBits = bits) {IntPtr ptr = new IntPtr (pBits); System. drawing. bitmap bitmap = new System. drawing. bitmap (width, height, stride, System. drawing. imaging. pixelFormat. format32bppPArgb, ptr); return bitmap ;}}}
To change the properties of a project, select "allow unsafe code" in project properties \ "generate"
For how to obtain the QQ Emoji name and bind it to RichTextBox, refer to the source code of the address blogger pointed to in the first line.
System. Drawing. Bitmap can directly save the file or convert it to System. Drawing. Imgage. This is relatively simple and you don't know Baidu.
Append the image or text to RichTextBox.
Image:
BlockUIContainer bl = new BlockUIContainer (); System. windows. controls. image imgs = new System. windows. controls. image (); System. drawing. bitmap bitmap = new System. drawing. bitmap (System. drawing. image); BitmapImage bitmapImage = new BitmapImage (); using (System. IO. memoryStream MS = new System. IO. memoryStream () {bitmap. save (MS, ImageFormat. png); bitmapImage. beginInit (); bitmapImage. streamSource = The MS; bitmapImage. cacheOption = BitmapCacheOption. onLoad; bitmapImage. endInit (); bitmapImage. freeze ();} imgs. source = bitmapImage; imgs. width = Convert. toDouble (1, 150); imgs. height = Convert. toDouble (1, 100); bl. child = imgs; RichTextBox. document. blocks. add (bl); RichTextBox. focus (); System. windows. forms. sendKeys. sendWait ("^ {END}"); // move the cursor to the END
Text:
Paragraph par = new Paragraph (); par. inlines. add (new Run ("text"); RichTextBox. document. blocks. add (par); RichTextBox. focus (); System. windows. forms. sendKeys. sendWait ("^ {END }");