浏览代码

ComPDFKit.Tool(Win) - 图章修复设置透明度为0并保存后Undo,Redo图章内容变白问题

liyuxuan 5 月之前
父节点
当前提交
68109bcd26
共有 1 个文件被更改,包括 66 次插入11 次删除
  1. 66 11
      Demo/Examples/ComPDFKit.Tool/UndoManger/AnnotHistory/StampAnnotHistory.cs

+ 66 - 11
Demo/Examples/ComPDFKit.Tool/UndoManger/AnnotHistory/StampAnnotHistory.cs

@@ -124,14 +124,14 @@ namespace ComPDFKit.Tool.UndoManger
 					default:
 						break;
 				}
-
-				stampAnnot.SetTransparency((byte)currentParam.Transparency);
-                string imagePath = string.Empty;
-                if (GetAnnotOpacityImage(stampAnnot, currentParam.Transparency / 255.0, 5, out imagePath) && File.Exists(imagePath))
-                {
-                    stampAnnot.SetImageStamp(imagePath, string.Empty);
-                    File.Delete(imagePath);
-                }
+                //stampAnnot.UpdateAp();
+                stampAnnot.SetTransparency((byte)currentParam.Transparency);
+                //string imagePath = string.Empty;
+                //if (GetAnnotOpacityImage(stampAnnot, currentParam.Transparency / 255.0, 5, out imagePath) && File.Exists(imagePath))
+                //{
+                //    stampAnnot.SetImageStamp(imagePath, string.Empty);
+                //    File.Delete(imagePath);
+                //}
 
                 if (!string.IsNullOrEmpty(currentParam.Author))
 				{
@@ -204,13 +204,13 @@ namespace ComPDFKit.Tool.UndoManger
 
                 if (updateParam.Transparency != checkParam.Transparency)
 				{
-					stampAnnot.SetTransparency((byte)updateParam.Transparency);
                     string imagePath = string.Empty;
                     if (GetAnnotOpacityImage(stampAnnot, updateParam.Transparency/255.0, 5, out imagePath) && File.Exists(imagePath))
                     {
                         stampAnnot.SetImageStamp(imagePath, string.Empty);
                         File.Delete(imagePath);
                     }
+                    stampAnnot.SetTransparency((byte)updateParam.Transparency);
                 }
 
 				if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
@@ -289,6 +289,19 @@ namespace ComPDFKit.Tool.UndoManger
                             continue;
                         }
 
+                        if (a == 0 && opacity > 0)
+                        {
+                            imageData[i * stride + j * 4] = 255;
+                            imageData[i * stride + j * 4 + 1] = 255;
+                            imageData[i * stride + j * 4 + 2] = 255;
+                            continue;
+                        }
+
+                        if (opacity == 0)
+                        {
+                            imageData[i * stride + j * 4 + 3] = 255;
+                            continue;
+                        }
                         imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
                     }
                 }
@@ -325,10 +338,41 @@ namespace ComPDFKit.Tool.UndoManger
                 RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)drawRect.Height, (int)drawRect.Width, 96, 96, PixelFormats.Pbgra32);
                 renderBitmap.Render(drawVisual);
                 string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
+                BitmapSource saveImageData = renderBitmap;
+                if (opacity == 0)
+                {
+                    stride = saveImageData.PixelWidth * 4;
+                    imageData = new byte[stride * saveImageData.PixelHeight];
+                    saveImageData.CopyPixels(imageData, stride, 0);
+                    for (int i = 0; i < saveImageData.PixelHeight; i++)
+                    {
+                        for (int j = 0; j < saveImageData.PixelWidth; j++)
+                        {
+                            byte b = imageData[i * stride + j * 4];
+                            byte g = imageData[i * stride + j * 4 + 1];
+                            byte r = imageData[i * stride + j * 4 + 2];
+                            byte a = imageData[i * stride + j * 4 + 3];
+
+                            if (a == 0 && b == 255 && g == 255 && r == 255)
+                            {
+                                continue;
+                            }
+
+                            if (a == 0 && b == 0 && g == 0 && r == 0)
+                            {
+                                continue;
+                            }
+                            imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
+                        }
+                    }
+                    writeImage = new WriteableBitmap(saveImageData.PixelWidth, saveImageData.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
+                    writeImage.WritePixels(new Int32Rect(0, 0, saveImageData.PixelWidth, saveImageData.PixelHeight), imageData, stride, 0);
+                    saveImageData = writeImage;
+                }
                 PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
                 using (FileStream fs = File.Create(tempPath))
                 {
-                    pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
+                    pngEncoder.Frames.Add(BitmapFrame.Create(saveImageData));
                     pngEncoder.Save(fs);
                 }
                 imagePath = tempPath;
@@ -357,6 +401,7 @@ namespace ComPDFKit.Tool.UndoManger
 
                 byte[] imageData = new byte[(int)drawRect.Width * (int)drawRect.Height * 4];
                 annot.RenderAnnot((int)drawRect.Width, (int)drawRect.Height, imageData);
+                byte annotOpacity = annot.GetTransparency();
 
                 int stride = ((int)drawRect.Width) * 4;
                 for (int i = 0; i < (int)(drawRect.Height); i++)
@@ -377,7 +422,17 @@ namespace ComPDFKit.Tool.UndoManger
                         {
                             continue;
                         }
-
+                        if (a == 0 && b == 0 && g == 0 && r == 0)
+                        {
+                            continue;
+                        }
+                        if (a == 0 && annotOpacity > 0)
+                        {
+                            imageData[i * stride + j * 4] = 255;
+                            imageData[i * stride + j * 4 + 1] = 255;
+                            imageData[i * stride + j * 4 + 2] = 255;
+                            continue;
+                        }
                         imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
                     }
                 }