Images in html export.

Is there a way to make sure that images rendered using the Dump() method are rendered properly in the html export?
For instance indicating a relative folder when executing LPRun where LPRun will store the images and these will be linked properly using relative paths in the resulting html?

To make sure the export is portable.

Comments

  • Here is one ugly way to achieve this (working images in exported result html).

    Method:
    - Convert image to base64 encoded string
    - Display image using data:image URI
    var client = new WebClient();
    var url = "http://www.gravatar.com/avatar/3b9fef24c50be8b7a6c8e7d0bae12485/?default=http%3A%2F%2Fvanillicon.com%2F3b9fef24c50be8b7a6c8e7d0bae12485_50.png&rating=g&size=50";
    Util.Image("data:image/png;base64," + Convert.ToBase64String(client.DownloadData(url))).Dump();
  • In my case the System.Drawing,Image object is returned from a library. But I can feed it into Util.Image() no problem.
    However, what does that achieve? The resulting html file still has an absolute path to "C:\Users\\AppData\Local\Temp\LINQPad5\something\something". If I want to copy the html file to another place, I can't take the image with it. For all I know, the image may already have been cleaned up and deleted.

    In short: this doesn't create a portable html file when exported:
    
    void Main()
    {
    	var img = Image.FromFile(@"C:\temp\someimage.gif");
    	Util.Image(img.imageToByteArray()).Dump();
    }
    
    // Define other methods and classes here
    static class Helpers
    {
        public static byte[] imageToByteArray(this System.Drawing.Image image)
    	{
    		using (var ms = new MemoryStream())
    		{
    			image.Save(ms, image.RawFormat);
    			return ms.ToArray();
    		}
    	}
    	
    }
    
  • Change this:
    Util.Image(img.imageToByteArray()).Dump();
    Into this:
    Util.Image("data:image/gif;base64," + Convert.ToBase64String(img.imageToByteArray())).Dump();
    And the image will be embedded in the html.
  • nescafe is using the overload of Util.Image that takes a URI rather than a byte array, so there is no associated image file.
  • Oh, I see. I didn't realize that this was possible. Don't know html that well. That works. Thank you very much!
    I was looking for a way to designate a resources folder where those images would end up. But this is even better.
  • Using the newly introduced ToDump() extensions:
    void Main()
    {
      // Write code to test your extensions here. Press F5 to compile and run.
    }
    
    public static class MyExtensions
    {
      // Write custom extension methods here. They will be available to all queries.
    }
    
    // You can also define non-static classes, enums, etc.
    
    public static object ToDump(object input)
    {
      var image = input as System.Drawing.Image;
      if (image != null)
      {
        using (var ms = new MemoryStream())
        {
          image.Save(ms, image.RawFormat);
          return Util.Image("data:image;base64," + Convert.ToBase64String(ms.ToArray()));
        }
      }
      return input;
    }
    
    You can simply dump the Image object:
    var img = System.Drawing.Image.FromFile(@"C:\Windows\AppCompat\UA\GenericApp.png");
    img.Dump();
    Which results in the following HTML:
    image
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!