如何在第三张图像中合并两个不同的图像?

时间:2018-06-01 07:07:05

标签: c# winforms

我想在一张图片中合并两张不同的图片, 首先,我使用BarcodeLib.DLL生成BARCODE图像, 然后使用Bitmap Class生成SKULabelImage,现在我想设置 “SKULabelImage到BARCODE图像的底部”,但当我尝试合并这两个图像时 outputImage,它只打印BarcodeImage,我不知道哪里出错,请检查我的代码 给我建议。我在“Windows窗体”中创建了这个应用程序

 private void button1_Click(object sender, EventArgs e)
    {

        string Date = textBox1.Text;
        string Number = textBox2.Text;
        if (Number != "")
        {

            int NoofLables = Convert.ToInt32(Number);

            for (int i = 1; i <= NoofLables; i++)
            {

                string Filename = "Barcodeimage" + i + ".jpg";

                string imgsavepath = "E:\\Pankaj\\BarcodeDemo\\BarcodeDemo\\BarcodeImage\\" + Filename;

               // string imgSkupath = "E:\\Pankaj\\BarcodeDemo\\BarcodeDemo\\BarcodeImage\\" + "FinalImg.jpg";
                BarcodeLib.SaveTypes savetype = BarcodeLib.SaveTypes.UNSPECIFIED;
                savetype = BarcodeLib.SaveTypes.JPG;
                System.IO.FileStream MemStream = new FileStream(imgsavepath, FileMode.Create, FileAccess.Write);

                System.Drawing.Image barcodeImage = null;
                Bitmap FinalImage = null;
                BarcodeLib.Barcode b = new BarcodeLib.Barcode();


                //b.IncludeLabel = true;
                b.LabelFont = new Font("Arial", 5);

                string sku = "SKU:VXN4214IN";
                string StringToEncode = "16280/" + i+' '+ sku;


                //BARCODE IMAGE
                barcodeImage = b.Encode(BarcodeLib.TYPE.CODE128, "001234", Color.Black, Color.White, 113, 18);


                 //SKULableImage
                Bitmap SkuImage = new Bitmap(113, 18, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                RectangleF rectf = new RectangleF(10, 10, 113, 18);
                Graphics graphics = Graphics.FromImage(SkuImage);

                graphics.DrawString(StringToEncode, new Font("Arial", 7), Brushes.Black, rectf);



                //Method For Meging Images
                // I Took outputImage Image in "FinalImage"
                FinalImage = MergeTwoImages(barcodeImage, SkuImage);




                 b.SaveImage(MemStream, savetype);

                MemStream.Close();
                FinalImage.Dispose();
            }

            label3.Text = "Images Generated..!";
            label3.Visible = true;
        }
        else
        {
            label3.Visible = true;
            label3.Text = "Please Enter No. Of Labels";
        }
    }




public static 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;

        int outputImageWidth = 226;

        int outputImageHeight = 50;


        //Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        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, 0));

            //graphics.DrawImage(firstImage, new Rectangle(new Point(0, 0), firstImage.Size),
            //   new Rectangle(new Point(0, 0), firstImage.Size), GraphicsUnit.Pixel);
            //graphics.DrawImage(secondImage, new Rectangle(new Point(0, firstImage.Height + 1), secondImage.Size),
            //    new Rectangle(new Point(), secondImage.Size), GraphicsUnit.Pixel);

            graphics.Dispose();
        }

        return outputImage;
    }

1 个答案:

答案 0 :(得分:1)

要解决此问题,请尝试使用此代码。第二个图像位置需要计算它。

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

我尝试过的示例代码。它按预期工作。

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