From 981923c2a2cff6d61cd5a474d2ba398ba48f1fd5 Mon Sep 17 00:00:00 2001
From: chr <chr@chr.com>
Date: 星期一, 02 九月 2024 17:40:17 +0800
Subject: [PATCH] 更新代码;

---
 CustomRictBox/CustomRichBox.xaml.cs |  135 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 134 insertions(+), 1 deletions(-)

diff --git a/CustomRictBox/CustomRichBox.xaml.cs b/CustomRictBox/CustomRichBox.xaml.cs
index ae3fec3..08bfa38 100644
--- a/CustomRictBox/CustomRichBox.xaml.cs
+++ b/CustomRictBox/CustomRichBox.xaml.cs
@@ -1,6 +1,11 @@
 锘縰sing Microsoft.Win32;
 using System;
+using System.ComponentModel;
+using System.Diagnostics;
 using System.IO;
+using System.Reflection;
+using System.Runtime.InteropServices.ComTypes;
+using System.Text;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Documents;
@@ -8,6 +13,7 @@
 using System.Windows.Markup;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
+using System.Windows.Media.Media3D;
 
 namespace PdmSwPlugin.UI
 {
@@ -285,11 +291,16 @@
                 {
                     width = picMaxWidth;
                 }
+                TextPointer pointer = richTextBox.CaretPosition;
                 /// 琛屽唴鎻掑叆鍥剧墖
                 var insertImg = new Image() { Source = img, Stretch = Stretch.Uniform, Width = width };
                 var imgContainer = new InlineUIContainer(insertImg);
-                TextPointer pointer = richTextBox.CaretPosition;
                 pointer.Paragraph.Inlines.Add(imgContainer);
+
+                //string base64 = ImageToBase64(img);
+                //string imgXaml = $"<Image Source=\"data:image/jpg;base64,{base64}\" />";
+                //var imgContainer = new InlineUIContainer(imgXaml);
+                //pointer.Paragraph.Inlines.Add(imgXaml);
 
                 /// 涓嬮潰娉ㄩ噴鐨勮繖娈垫槸鐩存帴鎻掑叆鏂版钀界殑鍐欐硶
                 //var imgContainer = new BlockUIContainer(insertImg);
@@ -376,5 +387,127 @@
 
             }
         }
+
+
+
+        public void ToBase64Image2()
+        {
+            richTextBox.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
+            richTextBox.Arrange(new Rect(new Size(richTextBox.ActualWidth, richTextBox.ActualHeight)));
+
+            // Create a DrawingVisual and use a VisualBrush to render the RichTextBox
+            DrawingVisual visual = new DrawingVisual();
+            using (DrawingContext context = visual.RenderOpen())
+            {
+                VisualBrush brush = new VisualBrush(richTextBox);
+                context.DrawRectangle(brush, null, new Rect(new Size(richTextBox.ActualWidth, richTextBox.ActualHeight)));
+            }
+
+            // Render the DrawingVisual to a RenderTargetBitmap
+            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(
+                (int)richTextBox.ActualWidth,
+                (int)richTextBox.ActualHeight,
+                96d, 96d, PixelFormats.Pbgra32);
+
+            renderTargetBitmap.Render(visual);
+
+            // Create a BitmapEncoder to encode the RenderTargetBitmap
+            PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
+            pngEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
+            using (FileStream stream = new FileStream("C:\\Workspace\\test.png", FileMode.OpenOrCreate))
+            {
+                // Save the encoded PNG image to a memory stream
+                pngEncoder.Save(stream);
+
+                stream.Flush();
+                stream.Close();
+            }
+
+           /* using (MemoryStream memoryStream = new MemoryStream())
+            {
+                // Save the encoded PNG image to a memory stream
+                pngEncoder.Save(memoryStream);
+                // Convert the memory stream to a byte array
+                byte[] imageBytes = memoryStream.ToArray();
+                // Convert the byte array to a Base64 string
+                return Convert.ToBase64String(imageBytes);
+            }*/
+        }
+
+        public string ToBase64Image()
+        {
+            //string sss = XamlWriter.Save(richTextBox.Document);
+            //Debug.Print(sss);
+            // Force a measure and arrange of the RichTextBox to ensure all content is rendered
+            //richTextBox.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
+            //richTextBox.Arrange(new Rect(new Size(richTextBox.ActualWidth, richTextBox.ActualHeight)));
+
+
+            //// Render the DrawingVisual to a RenderTargetBitmap
+            //RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(
+            //    (int)richTextBox.ActualWidth,
+            //    (int)richTextBox.ActualHeight,
+            //    96d, 96d, PixelFormats.Pbgra32);
+
+            //FlowDocumentReader reader = new FlowDocumentReader
+            //{
+            //    Document = richTextBox.Document
+            //};
+
+            //renderTargetBitmap.Render(reader);
+
+            //// Create a BitmapEncoder to encode the RenderTargetBitmap
+            //PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
+            //pngEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
+
+            //using (FileStream stream = new FileStream("C:\\Workspace\\test.png",FileMode.OpenOrCreate))
+            //{
+            //    // Save the encoded PNG image to a memory stream
+            //    pngEncoder.Save(stream);
+
+            //    stream.Flush();
+            //    stream.Close();
+            //}
+
+            //string sss = XamlWriter.Save(richTextBox.Document);
+            //Debug.Print(sss);
+            // Backup original size
+            var originalWidth = richTextBox.ExtentWidth;
+            var originalHeight = richTextBox.ExtentHeight;
+
+            //richTextBox.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
+            richTextBox.Arrange(new Rect(new Size(originalWidth, originalHeight)));
+
+            int dpi = 300;
+
+            // Create a RenderTargetBitmap with the new size
+            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(
+                (int)(originalWidth * dpi / 96),
+                (int)(originalHeight * dpi / 96),
+                300d, 300d, PixelFormats.Default);
+
+            // Render the control to the RenderTargetBitmap
+            renderTargetBitmap.Render(richTextBox);
+
+            // Create an encoder (PNG in this case)
+            PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
+            pngEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
+
+
+            FileStream pngfs = new FileStream("C:\\1.png", FileMode.OpenOrCreate);
+     
+            BitmapEncoder be = new PngBitmapEncoder();
+            be.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
+
+            using (MemoryStream ms = new MemoryStream())
+            {
+                be.Save(ms);
+                byte[] imageBytes = ms.ToArray();
+                return Convert.ToBase64String(imageBytes); // 灏嗗瓧鑺傛暟缁勮浆鎹负Base64瀛楃涓�
+            }
+
+            //be.Save(pngfs);
+            //pngfs.Close();
+        }
     }
 }

--
Gitblit v1.9.1