none
Converting text output to an image file(.jpeg) RRS feed

  • Question

  • Hi,

    I was wondering if it's possible to convert the content of a text file to an image file(.jpeg). I already have a code that exports the data to .txt format but I would like to have an image copy of the data if possible. Please advice

    • Moved by Bill_Stewart Tuesday, November 7, 2017 9:44 PM Off-topic
    Thursday, September 14, 2017 12:22 AM

All replies

  • There is not command that does that.  You can search on the Internet for programs that can convert text to image.


    \_(ツ)_/

    Thursday, September 14, 2017 12:30 AM
  • Thanks, Jrv.
    Thursday, September 14, 2017 2:31 AM
  • Of course it can be done with Net Framework classes if you are a sufficiently skilled programmer.

    Here is C# example: https://stackoverflow.com/questions/2070365/how-to-generate-an-image-from-text-on-fly-at-runtime


    \_(ツ)_/

    Thursday, September 14, 2017 2:36 AM
  • It is your luck day.  I decided to create a solution since it is really quite easy.

    $code = @'
    // https://stackoverflow.com/questions/2070365/how-to-generate-an-image-from-text-on-fly-at-runtime
    using System;
    using System.Drawing;
    
    public static class ImageUtils{
    	public static Image DrawText(String text, Font font, Color textColor, Color backColor) {
    		//first, create a dummy bitmap just to get a graphics object
    		Image img = new Bitmap(1, 1);
    		Graphics drawing = Graphics.FromImage(img);
    		
    		//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
    		img.Dispose();
    		drawing.Dispose();
    		
    		//create a new image of the right size
    		img = new Bitmap((int) textSize.Width, (int)textSize.Height);
    		
    		drawing = Graphics.FromImage(img);
    		
    		//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();
    		
    		return img;
    		
    	}
    }
    '@
    Add-Type $code -ReferencedAssemblies System.Drawing
    
    # use code
    $image = [ImageUtils]::DrawText('Hello World', 'Verdana, 24pt, style=Bold, Italic', 'Red', 'White')
    $image.Save("$pwd\mytext.jpg")
    . $pwd\mytext.jpg


    \_(ツ)_/


    • Edited by jrv Thursday, September 14, 2017 2:51 AM
    Thursday, September 14, 2017 2:51 AM
  • Really appreciate it, Jrv.

    Do you think it's possible to run these lines of code on an existing text file that has data in it? Basically, I wrote a script to export data to a text file but I also want the text file content to be in jpeg.

    Thursday, September 14, 2017 4:44 AM
  • just change the last bit to

    # use code
    $txt = get-content c:\test\results.txt
    $image = [ImageUtils]::DrawText($txt, 'Verdana, 24pt, style=Bold, Italic', 'Red', 'White')
    $image.Save("$pwd\mytext.jpg")

    Thursday, September 14, 2017 8:04 AM
  • Hi simbrook2,

    I already tried that and it came out as an empty .jpg file.

    Thursday, September 14, 2017 11:06 AM
  • Strange this worked for me

    $code = @'
    // https://stackoverflow.com/questions/2070365/how-to-generate-an-image-from-text-on-fly-at-runtime
    using System;
    using System.Drawing;

    public static class ImageUtils{
    public static Image DrawText(String text, Font font, Color textColor, Color backColor) {
    //first, create a dummy bitmap just to get a graphics object
    Image img = new Bitmap(1, 1);
    Graphics drawing = Graphics.FromImage(img);

    //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
    img.Dispose();
    drawing.Dispose();

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

    drawing = Graphics.FromImage(img);

    //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();

    return img;

    }
    }
    '@
    Add-Type $code -ReferencedAssemblies System.Drawing

    # use code
    $txt = get-content c:\test\results.txt
    $image = [ImageUtils]::DrawText($txt, 'Verdana, 24pt, style=Bold, Italic', 'Red', 'White')
    $image.Save("$pwd\mytext.jpg")
    . $pwd\mytext.jpg

    Thursday, September 14, 2017 1:33 PM
  • I'm not sure how this worked for you. I saved some data in the "c:\text\results.txt" and ran the script. The image file opened with "Windows photo viewer can't display this picture because the file is empty".

    Thursday, September 14, 2017 3:35 PM
  • I'm not sure how this worked for you. I saved some data in the "c:\text\results.txt" and ran the script. The image file opened with "Windows photo viewer can't display this picture because the file is empty".

    Did you look at the file to see if it had any length?

    If the line is too long for the screen it will cause an image error.  If you need to have justified text then the code has to be modified to apply an image size and to add an "StringFormat" specification to the output.

    See: https://msdn.microsoft.com/en-us/library/21kdfbzs(v=vs.110).aspx


    \_(ツ)_/

    Thursday, September 14, 2017 4:12 PM
  • The easiest way to convert formatted text to an image is to do a screen capture to a file.  OneNote can do this very easily.  Just type {WIN+SHIFT+S} and copy the selected image to a file. like this. 

    The following is an image:


    \_(ツ)_/


    • Edited by jrv Thursday, September 14, 2017 4:25 PM
    Thursday, September 14, 2017 4:25 PM