叠加两个不同类型的图像
我需要覆盖以下图像:
System.Windows.Controls.Image
image1System.Drawing.Bitmap
image2;
我需要输出类型为 System.Windows.Media.ImageSource 。
我想到了以下方法:将 image2 转换为 Bitmap 并使用 System.Drawing.Graphics 覆盖两个图像,但我不知道如何转换 image2。
I need to overlay the following images:
System.Windows.Controls.Image
image1System.Drawing.Bitmap
image2;
I need the output to be of type System.Windows.Media.ImageSource
.
I thought of the following way: Convert image2 into a Bitmap
and the overlay the two images using System.Drawing.Graphics
, but I don't know how to convert image2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是按照上面的建议将两个项目叠加在网格中,然后使用 此技术将控件呈现为位图文件。
或者,您可以将这两个文件转换为位图并使用以下代码循环它们(此代码通过将位图 2 分层到位图 1 上来组合图像,假设位图 2 中的任何白色值都是透明度值 - 您可以通过更改一行来更改此混合条件):
这将为您提供位图结果,然后您可以对其进行任何您想要的操作。
第一个选项速度更快,因为它是由 WPF 渲染代码加速的,但是我听说人们一直在努力强制渲染的位图不立即输出到文件,所以如果您希望它在内存中而不再次加载文件,那么它是不是你最好的选择。
第二个选项可以让您更好地控制图像的组合方式,但速度可能会较慢。
One option is overlaying the two items in a grid as suggested above and then using this technique to render the control to a bitmap file.
Alternatively, you could convert both files to a bitmap and loop over them using the following code (this code combines the images by layering bitmap2 on bitmap1, assuming any white values in bitmap2 are transparency values - you can change this blending condition by altering one line):
This will give you the Bitmap result, which you can then do whatever you want with.
The first option is faster, as it is accelerated by the WPF render code, however I've heard people have struggled to force the rendered bitmap to not output immediately to a file, so if you want it in memory without loading the file again it's not your best bet.
The second option gives you much more control over how the images are combined, but is potentially slower.