2

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: 2nd 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?

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.

Result

Could anyone give some suggestions please?

4

3 回答 3

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的文章

于 2013-05-15T04:29:00.820 回答
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/

于 2013-05-15T04:28:57.383 回答
0

这篇codeproject 文章展示了如何为带有文本的图像以及其他图像添加水印。

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

于 2013-05-15T04:56:08.597 回答