Bladeren bron

孙浩楠-添加图片功能

zhuyi 1 jaar geleden
bovenliggende
commit
35f16ce59d

+ 62 - 23
Demo/Examples/Compdfkit_Tools/Annotation/PDFAnnotationPanel/PDFAnnotationControl/CPDFAnnotationControl.xaml.cs

@@ -282,7 +282,7 @@ namespace Compdfkit_Tools.PDFControl
                         annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE;
                         byte[] LineColor = new byte[] { squareData.BorderColor.R, squareData.BorderColor.G, squareData.BorderColor.B };
                         (annotHandlerEventArgs as SquareParam).LineColor = LineColor;
-                        if(squareData.FillColor!=Colors.Transparent)
+                        if (squareData.FillColor != Colors.Transparent)
                         {
                             byte[] FillColor = new byte[] { squareData.FillColor.R, squareData.FillColor.G, squareData.FillColor.B };
                             (annotHandlerEventArgs as SquareParam).BgColor = FillColor;
@@ -307,11 +307,11 @@ namespace Compdfkit_Tools.PDFControl
                         annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE;
                         byte[] LineColor = new byte[] { cicleData.BorderColor.R, cicleData.BorderColor.G, cicleData.BorderColor.B };
                         (annotHandlerEventArgs as CircleParam).LineColor = LineColor;
-                        if(cicleData.FillColor!=Colors.Transparent)
+                        if (cicleData.FillColor != Colors.Transparent)
                         {
                             byte[] BgColor = new byte[] { cicleData.FillColor.R, cicleData.FillColor.G, cicleData.FillColor.B };
                             (annotHandlerEventArgs as CircleParam).BgColor = BgColor;
-                            (annotHandlerEventArgs as CircleParam).HasBgColor=true;
+                            (annotHandlerEventArgs as CircleParam).HasBgColor = true;
                         }
                         (annotHandlerEventArgs as CircleParam).LineWidth = cicleData.Thickness;
                         (annotHandlerEventArgs as CircleParam).Transparency = Convert.ToByte(cicleData.Opacity * 255);
@@ -468,7 +468,7 @@ namespace Compdfkit_Tools.PDFControl
                     break;
             }
             pdfViewerControl.PDFToolManager.SetToolType(ToolType.CreateAnnot);
-            if (annotationType!= CPDFAnnotationType.Stamp)
+            if (annotationType != CPDFAnnotationType.Stamp)
             {
                 pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(false);
             }
@@ -746,23 +746,62 @@ namespace Compdfkit_Tools.PDFControl
                     ClearPanel();
                     break;
                 case CPDFAnnotationType.Image:
-                    //StampAnnotArgs stampArgs = new StampAnnotArgs();
-                    //stampArgs.Opacity = 1;
-                    //stampArgs.Type = StampType.IMAGE_STAMP;
-                    //OpenFileDialog openFileDialog = new OpenFileDialog();
-                    //openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
-                    //if (openFileDialog.ShowDialog() == true)
-                    //{
-                    //    stampArgs.ImagePath = openFileDialog.FileName;
-
-                    //    this.pdfViewerControl?.SetMouseMode(MouseModes.AnnotCreate);
-                    //    this.pdfViewerControl?.SetToolParam(stampArgs);
-                    //}
-                    //else
-                    //{
-                    //    this.pdfViewerControl.SetMouseMode(MouseModes.PanTool);
-                    //}
-                    //ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
+                    StampParam stampParam = new StampParam();
+                    OpenFileDialog openFileDialog = new OpenFileDialog();
+                    openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
+                    if (openFileDialog.ShowDialog() == true)
+                    {
+                        try
+                        {
+                            BitmapImage image = new BitmapImage(new Uri(openFileDialog.FileName));
+                            PngBitmapEncoder encoder = new PngBitmapEncoder();
+                            encoder.Frames.Add(BitmapFrame.Create(image));
+                            MemoryStream memoryStream = new MemoryStream();
+                            encoder.Save(memoryStream);
+                            stampParam.ImageStream = memoryStream;
+                            stampParam.Transparency = 255;
+                            stampParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
+                            stampParam.StampType = C_STAMP_TYPE.IMAGE_STAMP;
+                            BitmapDecoder decoder = BitmapDecoder.Create(stampParam.ImageStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                            BitmapFrame frame = null;
+                            if (decoder != null && decoder.Frames.Count > 0)
+                            {
+                                frame = decoder.Frames[0];
+                            }
+                            if (frame != null)
+                            {
+                                byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
+                                if (frame.Format != PixelFormats.Bgra32)
+                                {
+                                    FormatConvertedBitmap covert = new FormatConvertedBitmap(
+                                        frame,
+                                        PixelFormats.Bgra32,
+                                        frame.Palette,
+                                        0);
+                                    covert.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
+                                }
+                                else
+                                {
+                                    frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
+                                }
+                                pdfViewerControl.PDFViewTool.GetCPDFViewer().SetStampMouseImage(imageArray, frame.PixelWidth, frame.PixelHeight);
+                                pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(true);
+                                pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(true);
+                            }
+                            DefaultSettingParam defaultSettingParam = pdfViewerControl.PDFViewTool.GetDefaultSettingParam();
+                            defaultSettingParam.SetAnnotParam(stampParam);
+                            pdfViewerControl.PDFToolManager.SetToolType(ToolType.CreateAnnot);
+                            pdfViewerControl.PDFToolManager.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
+                        }
+                        catch 
+                        {
+                        }
+                    }
+                    else
+                    {
+                        pdfViewerControl.PDFToolManager.SetToolType(ToolType.Pan);
+                        ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
+                    }
                     ClearPanel();
                     break;
                 default:
@@ -773,7 +812,7 @@ namespace Compdfkit_Tools.PDFControl
         private void PdfFreehandUI_EraseChangeHandler(object sender, double e)
         {
 
-            if (pdfViewerControl != null )
+            if (pdfViewerControl != null)
             {
                 pdfViewerControl.PDFViewTool.SetEraseZoom(e);
             }
@@ -975,7 +1014,7 @@ namespace Compdfkit_Tools.PDFControl
                                                                pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
                                                                baseAnnot.GetAnnotData().PageIndex,
                                                                baseAnnot.GetAnnotData().Annot);
-                        (tempAnnotationPanel as CPDFTempStampUI).SetPresentAnnotAttrib(annotParam as StampParam , baseAnnot.GetAnnotData().Annot as CPDFStampAnnotation , pdfViewerControl.PDFToolManager.GetDocument(), pdfViewerControl);
+                        (tempAnnotationPanel as CPDFTempStampUI).SetPresentAnnotAttrib(annotParam as StampParam, baseAnnot.GetAnnotData().Annot as CPDFStampAnnotation, pdfViewerControl.PDFToolManager.GetDocument(), pdfViewerControl);
                     }
                     break;
                 case C_ANNOTATION_TYPE.C_ANNOTATION_LINK: