From d7be195b0e1949c7b38bce2ad83ae974cbfac094 Mon Sep 17 00:00:00 2001 From: chr <chr@mailcom> Date: 星期三, 04 九月 2024 13:58:54 +0800 Subject: [PATCH] 优化图纸审核备注的图片上传; --- CustomRictBox/CustomRichBox.xaml.cs | 85 ++++++++++++++++++++++++++++++------------ 1 files changed, 61 insertions(+), 24 deletions(-) diff --git a/CustomRictBox/CustomRichBox.xaml.cs b/CustomRictBox/CustomRichBox.xaml.cs index 08bfa38..9592a35 100644 --- a/CustomRictBox/CustomRichBox.xaml.cs +++ b/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(); + } } } } -- Gitblit v1.9.1