简体   繁体   English

如何重叠图像并将其存储在新图像对象中

[英]How to overlap images and store them in new image objects

I have two images,now i want to overlay the images,such that the other image appears on center or left corner of the other image,and then when finally both the images are overlayed i can store it in another new image object,and i want to all this in code behind only not xaml,how to do this? 我有两个图像,现在我想覆盖图像,以使另一个图像出现在另一个图像的中心或左上角,然后当最终两个图像都覆盖时,我可以将其存储在另一个新的图像对象中,并且想要在代码背后只使用xaml而不是xaml,该怎么做?

if (((Grid)sender).Children.Count > 0)
{
    gridBackground = (ImageBrush)(((Grid)sender).Background);
    gridBackImage = new System.Windows.Controls.Image();
    gridBackImage.Source = gridBackground.ImageSource;
}

System.Windows.Controls.Image imgRejectIcon;
if (((Grid)sender).Children.Count > 0)
{
    imgRejectIcon = (System.Windows.Controls.Image)(((Grid)sender).Children[0]);    
}

Now i want to merge gridBackImage and imgRejection and store it in new image object 现在我想合并gridBackImage和imgRejection并将其存储在新的图像对象中

You can arrange your two Image s in any way that you want and I'll leave that code for you to do (it'd much simpler to do in XAML). 您可以按照需要的任何方式排列两个Image ,我将把该代码留给您执行(在XAML中这样做简单得多)。 In WPF, all UI controls extend the Visual class . 在WPF中, 所有 UI控件都扩展了Visual This is very useful for making BitmapImage s from UI controls, when used with the RenderTargetBitmap.Render method . RenderTargetBitmap.Render方法一起使用时,这对于从UI控件制作BitmapImage非常有用。

First, arrange your two Image controls into a Grid , or other container control, and then you can pass the container control to the RenderTargetBitmap.Render method and create an Image something like this: 首先,将您的两个Image控件安排到Grid或其他容器控件中,然后可以将容器控件传递给RenderTargetBitmap.Render方法,并创建一个类似于以下内容的Image

RenderTargetBitmap renderTargetBitmap = 
    new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(yourContainerControl); 
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (Stream fileStream = File.Create(filePath))
{
    pngImage.Save(fileStream);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM