From 31a636e735a0addc56e4f4527f500b7aa0874eb5 Mon Sep 17 00:00:00 2001
From: chr <chr@mailcom>
Date: 星期五, 03 一月 2025 11:21:46 +0800
Subject: [PATCH] 改了一大堆;

---
 CustomRictBox/CustomRichBox.xaml.cs |  192 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 181 insertions(+), 11 deletions(-)

diff --git a/CustomRictBox/CustomRichBox.xaml.cs b/CustomRictBox/CustomRichBox.xaml.cs
index ae3fec3..9592a35 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
 {
@@ -279,22 +285,38 @@
             if (DisableEdit) return;
             try
             {
-                double picMaxWidth = DProperty.GetPicMaxWidth(this);
+                double pageWidth = richTextBox.Document.PageWidth; //DProperty.GetPicMaxWidth(this);
                 double width = img.PixelWidth;
-                if (picMaxWidth > 0 && width > picMaxWidth)
+                if (double.IsNaN(pageWidth))
                 {
-                    width = picMaxWidth;
+                    double actWidth = richTextBox.ExtentWidth;
+                    if (width > actWidth)
+                    {
+                        width = pageWidth;
+                    }
                 }
-                /// 琛屽唴鎻掑叆鍥剧墖
-                var insertImg = new Image() { Source = img, Stretch = Stretch.Uniform, Width = width };
-                var imgContainer = new InlineUIContainer(insertImg);
+                else if (pageWidth > 0 && width > pageWidth)
+                {
+                    width = pageWidth;
+                }
                 TextPointer pointer = richTextBox.CaretPosition;
-                pointer.Paragraph.Inlines.Add(imgContainer);
 
-                /// 涓嬮潰娉ㄩ噴鐨勮繖娈垫槸鐩存帴鎻掑叆鏂版钀界殑鍐欐硶
-                //var imgContainer = new BlockUIContainer(insertImg);
-                //this.richTextBox.CaretPosition.InsertParagraphBreak();
-                //this.richTextBox.Document.Blocks.InsertAfter(this.richTextBox.CaretPosition.Paragraph, imgContainer);
+
+                var insertImg = new Image() { Source = img, Stretch = Stretch.Uniform, Width = width };
+                /// 琛屽唴鎻掑叆鍥剧墖
+                //var imgContainer = new InlineUIContainer(insertImg);
+                //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 InlineUIContainer(insertImg);
+                pointer.InsertParagraphBreak();
+                pointer.Paragraph.Inlines.Add(imgContainer);
+                // this.richTextBox.Document.Blocks.InsertAfter(this.richTextBox.CaretPosition.Paragraph, imgContainer);
             }
             catch (Exception e)
             {
@@ -376,5 +398,153 @@
 
             }
         }
+
+
+
+        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));
+
+            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瀛楃涓�
+            }
+        }
+
+        public byte[] GetBase64Blob(int dpi = 300)
+        {
+            richTextBox.BorderThickness = new Thickness(0);
+            var originalWidth = Math.Max(richTextBox.ExtentWidth, richTextBox.ActualWidth);
+            var originalHeight = Math.Max(richTextBox.ExtentHeight, richTextBox.ActualHeight);
+
+            richTextBox.Arrange(new Rect(new Size(originalWidth, originalHeight)));
+
+            // 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));
+
+            BitmapEncoder be = new PngBitmapEncoder();
+            be.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
+
+            richTextBox.BorderThickness = new Thickness(1);
+            using (MemoryStream ms = new MemoryStream())
+            {
+                be.Save(ms);
+                return ms.ToArray();
+            }
+        }
     }
 }

--
Gitblit v1.9.1