.NET:将剪贴板图像保存为 PNG 失败
我在将剪贴板图像保存为 png 时遇到一个奇怪的问题。当使用 prtscr 或截图工具截取屏幕截图时,它可以与 Clipboard.GetImage() 和 PngBitmapEncoder 配合使用。但是,当从画图复制图像并执行相同操作时,我得到一个空图像。至少在尝试查看它时它看起来是空的,尽管它似乎包含有效的 PNG 图像数据。使用 Jpeg 编码器时效果很好。 PNG 和 Paint 有什么问题?这和透明度有关系吗?
更新 澄清一下:我成功地从剪贴板中提取了图像。如果图像首先是从 Paint 复制的,我只是无法使用 PNGEncoder 将其保存到光盘。我可以使用 JpegEncoder 保存绘画剪贴板图像...但我想使用 PNG。
BitmapSource source = Clipboard.GetImage();
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(source));
enc.Save(stream);
上面的代码生成一个包含不可见内容的图像文件,根据查看者的不同,显示为完全黑色或白色(当从“画图”中按 ctrl-c 复制时)。这让我认为这与透明度有关......
I have a weird issue with saving of clipboard image to png. When taking a screenshot with prtscr or snipping tool it works fine with Clipboard.GetImage() and PngBitmapEncoder. But when copying the image from Paint and doing the same I get a empty image as result. At least it looks empty when trying to view it, though it seems to contain valid PNG-image-data. When using the Jpeg-encoder it works fine. Whats the problem with PNG and Paint? Does it have anything to do with transparency?
Update
to clarify: Im extracting an image successfully from the clipboard. I just cant save it to disc using the PNGEncoder if the image was copied from Paint in the first place. I can save paint-clipboard-images with the JpegEncoder though... but I want to use PNG.
BitmapSource source = Clipboard.GetImage();
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(source));
enc.Save(stream);
Above code produces a image-file with invisible content, turns up completely black or white depending on the viewer (when copied from, ctrl-c, from Paint). Which leads me to think it has something to do with transparency...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题看起来与我描述的问题非常相似
Your problem looks very similar to the one I described here... the solution I suggested should probably work for you.
我首先会尝试使用 ClipSpy 来检查哪个应用程序将哪种类型的内容放在上面剪贴板。
I would first try something like ClipSpy to examine which application puts which type of content on the clipboard.