Как объединить два разных изображения в третье изображение? - PullRequest
0 голосов
/ 01 июня 2018

Я хочу объединить два разных изображения в одном изображении. Прежде всего, я сгенерировал изображение BARCODE с помощью BarcodeLib.DLL, а затем сгенерировал SKULabelImage с помощью Bitmap Class, теперь я хочу установить «SKULabelImage в нижнее изображение BARCODE», но когда япопробуйте объединить эти два изображения в outputImage, он печатает только BarcodeImage, я не знаю, где это не так, пожалуйста, проверьте мой код, дайте мне предложения. Я создал это приложение в «Windows Form»

 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 голосов
/ 01 июня 2018

Для решения проблемы попробуйте этот код.вторая позиция изображения должна быть рассчитана.

    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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...