chr
2024-10-09 1f645778ae80a3a8801b8bb4d0fcf8feb244ad43
CustomRictBox/CustomRichBox.xaml.cs
@@ -285,27 +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;
                    }
                }
                else if (pageWidth > 0 && width > pageWidth)
                {
                    width = pageWidth;
                }
                TextPointer pointer = richTextBox.CaretPosition;
                /// 行内插入图片
                var insertImg = new Image() { Source = img, Stretch = Stretch.Uniform, Width = width };
                var imgContainer = new InlineUIContainer(insertImg);
                pointer.Paragraph.Inlines.Add(imgContainer);
                /// 行内插入图片
                //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 BlockUIContainer(insertImg);
                //this.richTextBox.CaretPosition.InsertParagraphBreak();
                //this.richTextBox.Document.Blocks.InsertAfter(this.richTextBox.CaretPosition.Paragraph, imgContainer);
                /// 图片另起一行
                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)
            {
@@ -423,15 +434,15 @@
                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);
            }*/
            /* 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()
@@ -493,9 +504,6 @@
            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));
@@ -505,9 +513,38 @@
                byte[] imageBytes = ms.ToArray();
                return Convert.ToBase64String(imageBytes); // 将字节数组转换为Base64字符串
            }
        }
            //be.Save(pngfs);
            //pngfs.Close();
        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();
            }
        }
    }
}