Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: Generate a new single image that repeats another image for x times horizontally

Tags:

c#

.net

image

I'm looking for sample .NET code (System.Drawing.Image) that does the following:

Load a given image file. Generate a new single image that repeats the orginal image for x times horizontally.

like image 220
Nick Avatar asked Jan 23 '26 04:01

Nick


1 Answers

This creates a new bitmap and draws the source bitmap to it numTimes times.

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();
like image 84
Ron Warholic Avatar answered Jan 25 '26 18:01

Ron Warholic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!