문제

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