-1

我有一個web應用程序,用戶將上傳帶有佈局的JPEG文件作爲我的示例,我需要動態添加文本到「標題」區域,併合並其他JPEG文件到「底部」區域。 是否可以用任何一種語言來做,.Net是首選。 The JPEG file layout如何添加文本或合併圖像到JPEG文件

+0

您可以使用圖像libaray做th是,例如imagemagick。 – DusteD

+0

是的,這兩種語言都可以使用。也許你應該問一個更具體的問題。 – Tim

回答

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); 
    } 
+0

我需要添加文本到現有的JPEG文件的「標題」區域,我需要在file1的「底部」區域合併JPEG文件2。 –

+0

@Simon - 我更新了我的帖子,添加了添加文本到現有JPEG圖像的代碼。請參考我的文章中的最後一個代碼塊。 –

+0

我需要找到「標題」和「底部」區域,它們都是矩形區域。 –

相關問題