将两个图像添加到一个图像中c#?

时间:2016-01-08 15:14:48

标签: c# image winforms

我想结合两张图片,但我不能成功。请帮帮我。

尝试合并PictureBox时只显示第一张图片但没有第二张图片,当我删除第一张图片时,我可以看到第二张图片。

此外,我尝试设置第一张图像并在图像上绘制文字,但这些文字也无效。请帮忙。

Image myimg = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);

Bitmap image = new Bitmap(myimg.Width + 20, myimg.Height + 50);
pictureBox1.DrawToBitmap(image, new Rectangle(0, 0, myimg.Width + 20, myimg.Height + 50));
Bitmap bmp = new Bitmap(myimg.Width + 20, myimg.Height);
Bitmap bmp2 = new Bitmap(myimg.Width + 20, 20);
Graphics Cizgi2 = Graphics.FromImage(bmp2);
Graphics Cizgi = Graphics.FromImage(bmp);
Cizgi.DrawImage(myimg, 0, 0);

FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
Cizgi2.DrawString(textBox1.Text, fonts, Brushes.Black, 5, myimg.Height + 10);

Graphics g = Graphics.FromImage(image);
g.DrawImage(bmp, new Point(10, 0));
g.DrawImage(bmp2, new Point(0, bmp.Height + 10));

I want to image seems like first but i cant make

1 个答案:

答案 0 :(得分:1)

看起来你试图垂直连接两个图像?实际上非常简单,您可以在这里查看(C# image concatenation),但我也根据您的需要对其进行了修改。我认为这应该有效:

        float drawBorderX = 5;
        float drawBorderY = 5;

        //Set up our two images
        Bitmap barCode = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
        Bitmap text = new Bitmap(barCode.Width, 50);
        Graphics textGraphics = Graphics.FromImage(text);


        //Draw the text to the bottom image.
        FontStyle sitil = FontStyle.Bold;
        Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
        textGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, text.Width, text.Height));
        textGraphics.DrawString(textBox1.Text, fonts, Brushes.Black, drawBorderX, drawBorderY);

        //Vertically concatenate the two images.
        Bitmap resultImage = new Bitmap(Math.Max(barCode.Width, text.Width), barCode.Height + text.Height);
        Graphics g = Graphics.FromImage(resultImage);
        g.DrawImage(barCode, 0, 0);
        g.DrawImage(text, 0, barCode.Height);

编辑:请注意,resultImage将包含您想要的图像,因此您可以将PictureBox设置为最终的图像。