I'm coding a game in a Console Application. I need to create a Bitmap file (targetfile) from multiple Bitmap files (sourcefiles) by positioning the sourcefiles in different places of the targetfile. What I need is a method like the following:

void Copy(Bitmap targetfile, Bitmap sourcefile, int position_x, int position_y)
{
   //Copy sourcefile into the (position_x, position_y) of the targetfile.
}

I searched but had no luck. Any ideas on how to do this?

有帮助吗?

解决方案

You can use the Graphics class in System.Drawing.

using System.Drawing;

// Then, in your class

public static void Copy (Bitmap target, Bitmap source, int x, int y)
{
    Graphics g=Graphics.FromImage(target);
    g.DrawImage(source, x, y);
}
public static void Main (string[] args)
{
    Bitmap target=(Bitmap)Bitmap.FromFile("bg.jpg");
    Bitmap source=(Bitmap)Bitmap.FromFile("fg.png");
    Copy (target, source, 100,50);
    Copy (target, source, 200,300);
    Copy (target, source, 500,450);
    target.Save("newBG.jpg");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top