使用C#合并2个图像

时间:2013-05-15 04:24:49

标签: c# image-processing watermark

我想在我的C#程序中合并两张图片。 第一个是灰度模式下的任何图片,第二个是这张图片中的图片:2nd Picture

两张图片/图片都有相同的尺寸,这是我的代码:

Bitmap first = new Bitmap (picturebox1.image);
Bitmap second = new Bitmap (picturebox2.image);
Bitmap result = new Bitmap (first.width, first.height);
Graphics g = Graphics.FromImage(result);
g.DrawImageUnscaled(first, 0, 0);
g.Flush();
g.DrawImageUnscaled(second, 0, 0);
g.Flush();
picturebox3.image = result;

我可以加入这些图片,但结果的尺寸小于两张原件(两张图片尺寸相同)。有人能给我一些建议吗?

另外,我希望结果图片的条件如下:  如果第二张照片中的边缘像素落到第一张照片的亮侧,则会变暗;否则当边缘落到暗侧时,它会变亮(看起来像是发光)。 所以文字将是半透明的。

以下是我想要的结果示例。

Result

有人可以提出一些建议吗?

3 个答案:

答案 0 :(得分:5)

这是为了加入

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);

尝试将它们合并到另一个上面。自己设置alpha(红色:如果你不想要alpha,你可以使用BitMap.MakeTransParent)

        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" );
        }
    }
}

并且为了水印你为什么不想使用带有alpha的Drawstring 这是所有这些http://www.codeproject.com/Articles/5034/How-to-implement-Alpha-blending

的文章

答案 1 :(得分:0)

您需要包含System.Drawing.Imaging命名空间才能使此代码正常工作。

浏览以下代码:

private void CombineImages(FileInfo[] files)
{
    //change the location to store the final image.
    string finalImage = @"C:\\MyImages\\FinalImage.jpg";
    List imageHeights = new List();
    int nIndex = 0;
    int width = 0;
    foreach (FileInfo file in files)
    {
        Image img = Image.FromFile(file.FullName);
        imageHeights.Add(img.Height);
        width += img.Width;
        img.Dispose();
    }
    imageHeights.Sort();
    int height = imageHeights[imageHeights.Count - 1];
    Bitmap img3 = new Bitmap(width, height);
    Graphics g = Graphics.FromImage(img3);
    g.Clear(SystemColors.AppWorkspace);
    foreach (FileInfo file in files)
    {
        Image img = Image.FromFile(file.FullName);
        if (nIndex == 0)
        {
            g.DrawImage(img, new Point(0, 0));
            nIndex++;
            width = img.Width;
        }
        else
        {
            g.DrawImage(img, new Point(width, 0));
            width += img.Width;
        }
        img.Dispose();
    }
    g.Dispose();
    img3.Save(finalImage, System.Drawing.Imaging.ImageFormat.Jpeg);
    img3.Dispose();
    imageLocation.Image = Image.FromFile(finalImage);
}

关注链接:

http://www.niteshluharuka.com/2012/08/combine-several-images-to-form-a-single-image-using-c/

答案 2 :(得分:0)

codeproject article显示了如何使用文本和其他图像为图像添加水印。

总之,您需要做的是在图像上以所需的透明度绘制水印图像。