工单 修复 微信复制图片上传不展示的bug

This commit is contained in:
chfei committed 2026-10-21 12:17:24 +08:00
1 parent 9c62e0c03d
commit 1d5b6ba0e4
2 files changed
+69 -10

No files matched your search

@@ -207,7 +207,7 @@ public sealed class PendingAttachmentViewModel : BindableBaseEx, IDisposable
Path.GetTempPath(),
"WxOrder-Clipboard-" + Guid.NewGuid().ToString("N") + ".png");
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Frames.Add(BitmapFrame.Create(NormalizeClipboardBitmap(bitmap)));
using (var stream = File.Create(filePath))
encoder.Save(stream);
@@ -236,6 +236,29 @@ public sealed class PendingAttachmentViewModel : BindableBaseEx, IDisposable
}
}
private static BitmapSource NormalizeClipboardBitmap(BitmapSource bitmap)
{
if (bitmap.Format != PixelFormats.Bgra32 && bitmap.Format != PixelFormats.Pbgra32)
return bitmap;
var stride = checked(bitmap.PixelWidth * 4);
var pixels = new byte[checked(stride * bitmap.PixelHeight)];
bitmap.CopyPixels(pixels, stride, 0);
for (var i = 3; i < pixels.Length; i += 4)
{
if (pixels[i] != 0)
return bitmap;
}
// 微信等截图工具可能将 DIB 的保留字节全部置零,WPF 将其解释为 Alpha。
// 直接按 Bgr32 使用原始颜色,避免预乘 Alpha 转换时丢失 RGB;正常透明图保持原样。
var normalized = BitmapSource.Create(
bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY,
PixelFormats.Bgr32, null, pixels, stride);
normalized.Freeze();
return normalized;
}
public void Dispose()
{
if (_disposed)