Merging two images in C/.NET
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Merging two images in .NET usually means one of two things: placing them side by side on a larger canvas, or drawing one on top of the other as an overlay. The implementation is straightforward once you decide which composition you want. The core steps are to create a destination bitmap, draw both source images into it, and save the result.
Compose images on a new bitmap
A common Windows-oriented approach uses System.Drawing:
This creates a side-by-side composition. The destination image width is the sum of the two source widths, and the height is the taller of the two.
Overlay one image on another
If you want one image drawn on top of another instead of beside it, change the canvas size and draw coordinates:
This is the pattern to use for watermarks, icons, and badge overlays.
Decide how to handle size mismatches
Merging images always raises layout questions:
- should the images keep original size
- should one be resized first
- what should fill empty background space
- should transparency be preserved
If the images do not naturally align, resize them before drawing or choose a canvas color that makes the final composition acceptable.
For example, you can scale an image during drawing by passing a destination rectangle with a different size.
Know the platform tradeoff
System.Drawing is familiar and easy for simple composition tasks, but it is not the best cross-platform choice for every modern .NET application. If you need a cross-platform image pipeline, a library such as ImageSharp may be a better fit. The core idea is the same, though: create a canvas and draw or composite both images onto it.
So the question is less "how do I merge two images" and more "which graphics library fits my runtime and deployment target." For small Windows tools, System.Drawing is often enough.
Preserve transparency intentionally
If you care about alpha transparency, use an image format such as PNG for the output. If you save a transparent composition to JPEG, the alpha channel is lost.
Also be deliberate about the background clear color. If you want transparency in empty areas, use a destination bitmap format and clear behavior that preserves alpha rather than painting a solid background.
Common Pitfalls
The most common mistake is drawing both images onto one of the original bitmaps directly instead of creating a destination canvas sized for the final output.
Another common issue is saving to JPEG when the composition depends on transparency. JPEG does not preserve alpha.
People also ignore mismatched image sizes and then wonder why the result is cropped or padded awkwardly.
Finally, if the application must run cross-platform, check whether the chosen .NET graphics API is appropriate before building the rest of the pipeline around it.
Summary
- Create a destination bitmap sized for the final merged output.
- Draw both source images onto that bitmap with
Graphics.DrawImage. - Use different coordinates for side-by-side versus overlay composition.
- Handle size mismatches and transparency intentionally.
- Choose the image library based on whether the application is Windows-only or cross-platform.
Related reading
- Methods to feed multiple images of same object to neural network for object detection
- Miminum requirements for Google tensorflow image classifier
- Miminum requirements for Google tensorflow image classifier
- Mnist dataset splitting
- Metadata file '.dll' could not be found
- Method overloading vs optional parameters in C 4.0
- MobileNet vs SqueezeNet vs ResNet50 vs Inception v3 vs VGG16
- Mobilenet vs SSD

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.