Merging Two Images in C#/.Net

Merging two images in C#/.NET

basically i use this in one of our apps:
we want to overlay a playicon over a frame of a video:

Image playbutton;
try
{
playbutton = Image.FromFile(/*somekindofpath*/);
}
catch (Exception ex)
{
return;
}

Image frame;
try
{
frame = Image.FromFile(/*somekindofpath*/);
}
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(/*somekindofpath*/,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex) { }
}
}

Merging 2 images using C#

It was for joining

Bitmap first = new Bitmap (picturebox1.Image);
Bitmap second = new Bitmap (picturebox2.Image);
Bitmap result = new Bitmap (first.Width+first.Width, first.Height);
Graphics g = Graphics.FromImage(result);
g.DrawImageUnscaled(first, 0, 0);
g.DrawImageUnscaled(second,first.Width, 0);

Try this for merging one on top another . set alpha by yourself ( red: U can use BitMap.MakeTransParent if u not want alpha)

        public Bitmap SetImageOpacity(Image image, float opacity)
{
try
{
//create a Bitmap the size of the image provided
Bitmap bmp = new Bitmap(image.Width, image.Height);

//create a graphics object from the image
using (Graphics gfx = Graphics.FromImage(bmp))
{

//create a color matrix object
ColorMatrix matrix = new ColorMatrix();

//set the opacity
matrix.Matrix33 = opacity;

//create image attributes
ImageAttributes attributes = new ImageAttributes();

//set the color(opacity) of the image
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

//now draw the image
gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
}
return bmp;
}
catch (Exception ex)
{

return null;
}
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap first = new Bitmap(pictureBox1.Image);
Bitmap second = SetImageOpacity(pictureBox2.Image, 0.5f);
//Bitmap result = new Bitmap(first.Width, first.Height);
//fix :
Bitmap result = new Bitmap(Math.Max(first.Width,second.Width), Math.Max(first.Height,second.Height));
Console.WriteLine(first.Width);
Graphics g = Graphics.FromImage(result);
g.DrawImageUnscaled(first, 0, 0);
g.DrawImageUnscaled(second, 0, 0);
pictureBox3.Image = result;
result.Save("result.jpg" );
}
}
}

And Coming For watermark why not you want to use Drawstring with alpha
here is article for all these http://www.codeproject.com/Articles/5034/How-to-implement-Alpha-blending

How to combine two images?

You can draw your 2 source images onto a new image of the correct dimensions using this code.

It takes your 2 source images, resizes them down to the exact dimensions required, then draws each of them onto a third image ready for saving.

using (Image<Rgba32> img1 = Image.Load<Rgba32>("source1.png")) // load up source images
using (Image<Rgba32> img2 = Image.Load<Rgba32>("source2.png"))
using (Image<Rgba32> outputImage = new Image<Rgba32>(200, 150)) // create output image of the correct dimensions
{
// reduce source images to correct dimensions
// skip if already correct size
// if you need to use source images else where use Clone and take the result instead
img1.Mutate(o => o.Resize(new Size(100, 150)));
img2.Mutate(o => o.Resize(new Size(100, 150)));

// take the 2 source images and draw them onto the image
outputImage.Mutate(o => o
.DrawImage(img1, new Point(0, 0), 1f) // draw the first one top left
.DrawImage(img2, new Point(100, 0), 1f) // draw the second next to it
);

outputImage.Save("ouput.png");
}

This code assumes you have these usings in scope

using SixLabors.ImageSharp.Processing.Transforms;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing.Drawing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;

How to Merge Two Differnt Images in Third Image?

To solve the issue try with this code. the second image position need to be calculate it.

    public Bitmap MergeTwoImages(Image firstImage, Bitmap secondImage)
{
if (firstImage == null)
{
throw new ArgumentNullException("firstImage");
}

if (secondImage == null)
{
throw new ArgumentNullException("secondImage");
}

int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width;
int outputImageHeight = firstImage.Height + secondImage.Height + 1;
Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight);
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.DrawImage(firstImage, new Point(0, 0));
graphics.DrawImage(secondImage, new Point(0, firstImage.Height + 1));
}

return outputImage;
}

sample code which i was tried. its worked as expected.

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();

Bitmap one = new Bitmap(113, 18);
using (Graphics g = Graphics.FromImage(one))
{
g.FillRectangle(Brushes.Red, 0, 0, 113, 18);
g.DrawString("ONE", new Font("Arial", 7), Brushes.Black, new RectangleF(0, 0, 113, 18), new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
}
Bitmap two = new Bitmap(113, 18, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(two))
{
g.FillRectangle(Brushes.Yellow, 0, 0, 113, 18);
g.DrawString("TWO", new Font("Arial", 7), Brushes.Black, new RectangleF(0, 0, 113, 18), new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
}

BackgroundImageLayout = ImageLayout.Center;
BackgroundImage = MergeTwoImages(one, two);
}

public Bitmap MergeTwoImages(Image firstImage, Bitmap secondImage)
{
if (firstImage == null)
{
throw new ArgumentNullException("firstImage");
}

if (secondImage == null)
{
throw new ArgumentNullException("secondImage");
}

int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width;
int outputImageHeight = firstImage.Height + secondImage.Height + 1;
Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight);
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(firstImage, new Point(0, 0));
graphics.DrawImage(secondImage, new Point(0, firstImage.Height + 1));
}

return outputImage;
}
}

How can merge two images to be one image with the two images combined one transparent over the second one?

Assuming bmp1 has alpha in it, draw bmp2 first, THEN with compositing mode set to SourceOver (the default) draw bmp1. This should accomplish the correct alpha blend order.

In other words...

Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp2,bmp1)); // Swapped arguments.

If bmp1 does not contain alpha, you will need to use a color matrix to change the transparency.



Related Topics



Leave a reply



Submit