如何添加文本或将图像合并到JPEG文件

时间:2013-11-28 11:15:10

标签: python .net image-processing machine-learning pattern-matching

我有一个Web应用程序,用户将上传带有布局的JPEG文件作为我的样本,我需要动态地将文本添加到“标题”区域,并将其他JPEG文件合并到“底部”区域。 是否可以使用任何一种语言来实现,.Net是首选。 The JPEG file layout

1 个答案:

答案 0 :(得分:0)

使用以下c#代码从文本创建jpeg图像:

    /// <summary>
    /// Creates a Jpeg image file drawing the given text and saves the file on give disk location.
    /// </summary>
    /// <param name="text">text which is to be draw in image</param>
    /// <param name="font">font is to be used for the text</param>
    /// <param name="textColor">color to be used for the text</param>
    /// <param name="backColor">background color to be used for the image</param>
    /// <param name="filename">filename with complete path where you want to save the output jpeg file.</param>
    private static void DrawText(String text, Font font, Color textColor, Color backColor, string filename)
    {
        //first, create a dummy bitmap just to get a graphics object
        Image image = new Bitmap(1, 1);
        Graphics drawing = Graphics.FromImage(image);

        //measure the string to see how big the image needs to be
        SizeF textSize = drawing.MeasureString(text, font);

        //free up the dummy image and old graphics object
        image.Dispose();
        drawing.Dispose();

        //create a new image of the right size
        image = new Bitmap((int)textSize.Width, (int)textSize.Height);

        drawing = Graphics.FromImage(image);

        //paint the background
        drawing.Clear(backColor);

        //create a brush for the text
        Brush textBrush = new SolidBrush(textColor);

        drawing.DrawString(text, font, textBrush, 0, 0);

        drawing.Save();

        textBrush.Dispose();
        drawing.Dispose();

        image.Save(filename, ImageFormat.Jpeg);
    }

使用以下代码垂直合并两个jpeg图像:

    /// <summary>
    /// Merges two Jpeg images vertically
    /// </summary>
    /// <param name="inputJpeg1">filename with complete path of the first jpeg file.</param>
    /// <param name="inputJpeg2">filname with complete path of the second jpeg file.</param>
    /// <param name="outputJpeg">filename with complete path where you want to save the output jpeg file.</param>
    private void MergeJpeg(string inputJpeg1, string inputJpeg2, string outputJpeg)
    {

        Image image1 = Image.FromFile(inputJpeg1);
        Image image2 = Image.FromFile(inputJpeg2);

        int width = Math.Max(image1.Width, image2.Width);
        int height = image1.Height + image2.Height;

        Bitmap outputImage = new Bitmap(width, height);
        Graphics graphics = Graphics.FromImage(outputImage);

        graphics.Clear(Color.Black);
        graphics.DrawImage(image1, new Point(0, 0));
        graphics.DrawImage(image2, new Point(0, image1.Height));

        graphics.Dispose();
        image1.Dispose();
        image2.Dispose();

        outputImage.Save(outputJpeg, System.Drawing.Imaging.ImageFormat.Jpeg);
        outputImage.Dispose();
    }

使用以下代码在现有Jpeg图像中添加文本:

    var inputFile = @"c:\inputImage.jpg";
    var outputFile = @"c:\outputImage.jpg";
    Bitmap bitmap = null;

    //Open file in read moad and create a stream and using that stream create a bitmap.
    using (var stream = File.OpenRead(inputFile))
    {
        bitmap = (Bitmap)Bitmap.FromStream(stream);
    }

    using (bitmap)
    using (var graphics = Graphics.FromImage(bitmap))
    using (var font = new Font("Arial", 20, FontStyle.Regular))
    {
        //Draw Title in the existing jpeg file at coordinates 0,0 these coordinates can be changed as per your need.
        graphics.DrawString("Title ", font, Brushes.Red, 0, 0);

        //Save the updated file.
        bitmap.Save(outputFile);
    }
相关问题