chr
2024-08-30 3d4d3fc5b2ef7fc3904e2e79f0c0896241919958
CustomRictBox/CustomRichBox.xaml.cs
@@ -1,6 +1,11 @@
using 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();
        }
    }
}