简体   繁体   中英

How to merge two images in one?

how can I merge canvas in one image ? I need do this because I want to save merge image.

Here is my code:

WriteableBitmap wb = new WriteableBitmap(50, 50);
wb.LoadJpeg(stream);
Image t = new Image();
t.Source = wb;
Canvas.SetLeft(t, 130);
Canvas.SetTop(t, 130);
canvas1.Children.Add(t);

So now I want to merge these two images into one and use my save function.

You can use Graphics.FromImage() and Graphics.DrawImage()

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/42807xh1%28v=vs.110%29.aspx

// Pseudo ...
using (Graphics gfx = Graphics.FromImage(image1))
{
    gfx.DrawImage(image2, new Point(0, 0));
}

// image1 is now merged with image 2

You have not specified in details what do you mean by "merge". Do you mean, overlay the images on top of each other (if that's the case, what overlay mode? add? multiply? normal?) or merge the images side by side into a larger image (like taking 3 shots with a camera and then combining them into one long photo)? Either way, you will want to look at the System.Drawing namespace.

Assuming the latter one is the case. Here's what you'll do:

Image a = ...;
Image b = ...;
//assuming equal height, and I forget whether the ctor is width first or height first
Image c = new Image(a.Width + b.Width, a.Height);
Graphics g = Graphics.FromImage(c);
g.DrawImage(...); //a lot of overloads, better check out the documentation
SaveImage(c); //depending on how you want to save it
g.Dispose();

You need a third-party library, like WriteableBitmapEx . Look at the Blit method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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