合并使用C#2图像 [英] Merging 2 images using C#

查看:135
本文介绍了合并使用C#2图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的C#程序合并两个图片。
中的第一个是在灰度模式中的任何图像,并且第二个是像在这样的画面:

I want to merge two pictures in my C# program. the first one is any picture in grayscale mode, and the second one is like in this picture:

两者的图片/图像具有相同的尺寸,这是我的代码:

Both of the pictures/images have the same size, and this is my code:

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;



我可以加入那些画面,但结果比两张原稿尺寸较小(包括图片有相同尺寸)。 ?谁能给我一些建议。

I can join those picture, but the result has smaller size than the two originals (both pictures have same size). Could anyone give me some suggestions?

此外,我想要的结果画面有这样的情况:
如果在第二个画面的边缘像素降至光明的一面在第1的,这将是黑暗的,否则当边缘下降到黑暗的一面,这将是光明的(好像发光)。
这样的文本将半透明。

Additionally, I want the result picture has condition like this : if the edge pixel in 2nd picture dropped to the bright side at the 1st one, it will be dark, otherwise when the edge dropped to the dark side, it will be bright (seem glow). so the text will be semi transparent.

下面是我想要的结果的一个例子。

Here's an example of the results I want.

谁能给一些建议好吗?

推荐答案

这是加入

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



试试这个合并一个在顶部的另一个。自己设置阿尔法(红色:U可以使用BitMap.MakeTransParent如果妳不想要阿尔法)

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



和未来的对水印为什么不想要使用抽绳与Alpha
这里是文章,所有这些的 http://www.codeproject.com/Articles/5034/How-to-implement-Alpha-blending

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

这篇关于合并使用C#2图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆