Home > Backend Development > C++ > How to Effortlessly Merge Two Images in C#/.NET?

How to Effortlessly Merge Two Images in C#/.NET?

Linda Hamilton
Release: 2025-01-04 20:18:42
Original
975 people have browsed it

How to Effortlessly Merge Two Images in C#/.NET?

Merging Two Images Effortlessly in C#/.NET

Background:

Envision merging two images: one spanning 500x500 pixels with a transparent center and the other measuring 150x150 pixels. The objective is to create a 500x500 canvas, position the smaller image in its middle, and overlay the larger image such that the transparent area reveals the underlying image. This seemingly simple task may require some guidance in C#.

Solution:

C# provides versatile classes and methods for image manipulation. To merge two images, we embark on the following steps:

  1. Instantiate the necessary objects from the System.Drawing namespace: Image, Bitmap, and Graphics.
  2. Define the image properties (width, height, source paths).
  3. Retrieve the images using the Image.FromFile() method.
  4. Create a Bitmap representing the canvas using the designated width and height.
  5. Acquire a Graphics object from the Bitmap.
  6. Set the Graphics Interpolation Mode to ensure high-quality image rendering.
  7. Draw the larger image onto the canvas, specifying the destination and source rectangles.
  8. Draw the smaller image at the desired position within the canvas.
  9. Save the merged image using the Bitmap.Save() method.

Code Example:

The following C# code snippet demonstrates the image merging process:

using System.Drawing;

Image playbutton, frame;
try
{
    playbutton = Image.FromFile(/*larger image path*/);
    frame = Image.FromFile(/*smaller image path*/);
}
catch (Exception ex)
{
    return;
}

using (frame)
{
    using (var bitmap = new Bitmap(width, height))
    {
        using (var canvas = Graphics.FromImage(bitmap))
        {
            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            canvas.DrawImage(frame, new Rectangle(0, 0, width, height), new Rectangle(0, 0, frame.Width, frame.Height), GraphicsUnit.Pixel);
            canvas.DrawImage(playbutton, (bitmap.Width / 2) - (playbutton.Width / 2), (bitmap.Height / 2) - (playbutton.Height / 2));
            canvas.Save();
        }
        try
        {
            bitmap.Save(/*merged image path*/, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex) { }
    }
}
Copy after login

By employing this approach, you can seamlessly merge two images in C#/.NET, empowering you to create visually stunning compositions.

The above is the detailed content of How to Effortlessly Merge Two Images in C#/.NET?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template