c#:生成一个新的单个图像,水平重复另一个图像x次

Nic*_*ick 3 .net c# image

我正在寻找执行以下操作的示例.NET代码(System.Drawing.Image):

加载给定的图像文件.生成一个新的单个图像,水平重复原始图像x次.

Ron*_*lic 7

这将创建一个新的位图并将源位图绘制到它的numTimes时间.

Bitmap b = Bitmap.FromFile(sourceFilename);
Bitmap output = new Bitmap(b.Width * numTimes, b.Height);
Graphics g = Graphics.FromImage(output);

for (int i = 0; i < numTimes; i++) {
  g.DrawImage(b, i * b.Width, 0);
}

// do whatever with the image, here we'll output it
output.Save(outputFilename);

// make sure to clean up too
g.Dispose();
b.Dispose();
output.Dispose();
Run Code Online (Sandbox Code Playgroud)

  • 您应该在using块中包装,而不是在g和b上显式调用Dispose. (3认同)