Solved

How can I show images using stringbuilder and htmltextwriter?

Posted on 2009-05-13
15
2,690 Views
Last Modified: 2012-05-06
The following code works to write some HTML for an outpur page I created.  The only factor that seems to be missing in the output are some of my images.  is there a wau to bring in the images as well?
StringBuilder   sb = new StringBuilder ();

                StringWriter    swr = new StringWriter (sb);

                HtmlTextWriter  hwr = new HtmlTextWriter (swr);

                base.Render (hwr);

                string html = sb.ToString ();

                writer.Write (html);

Open in new window

0
Comment
Question by:devnewbee
  • 8
  • 7
15 Comments
 
LVL 11

Expert Comment

by:azarc3
ID: 24376958
I assume you mean physical images that exist in the website you're outputting the page for, NOT a binary stream that would be rendered as an image.

You're going to need to make sure the images each have a valid path that is correct for the page you're rendering. For example...

This should work, assuming your images folder is in the root of the website:
sb.Append("<img src=\"/images/image1.jpg\" />");

This will NOT work, because HTML doesn't understand the significance of the tilde:
sb.Append("<img src=\"~/images/image1.jpg\" />");

This should work, assuming your images folder is in the root of the website:
sb.Append("<img src=\"" + ResolveUrl("~/images/image1.jpg") + "\" />");
0
 

Author Comment

by:devnewbee
ID: 24377001
So, if I use...

sb.Append("<img src=\"/files/image1.jpg\" />");

this should work?

Where is the stringbuilder syntax exactly do I place it and is there any other syntax required?
0
 
LVL 11

Expert Comment

by:azarc3
ID: 24377049
First, I 've got a question for you:

In your sample I don't see where you're actually populating the StringBuilder with your html code; did you simply not include that part in your question snippet? Otherwise, where is it coming from?
0
 

Author Comment

by:devnewbee
ID: 24377097
I am overiding the render.  Please see below...

    protected override void Render(HtmlTextWriter writer)


0
 
LVL 11

Expert Comment

by:azarc3
ID: 24377167
Which is applicable here?

A. The passed in HtmlTextWriter writer already has the HTML you're going to render
- OR -
B. You're going to create the HTML on the fly in your Render() method?
0
 

Author Comment

by:devnewbee
ID: 24377530
Its actually based on if the page is a postback and the selected value of a control.
0
 

Author Comment

by:devnewbee
ID: 24377619
B. You're going to create the HTML on the fly in your Render() method?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 11

Expert Comment

by:azarc3
ID: 24377851
Ok. As long as you're going to have the opportunity to inject the <img> code then this is an easy(ier) thing to do. Back to your first response, yes, the following will work.

sb.Append("<img src=\"/files/image1.jpg\" />");

The StringBuilder syntax that you'll want to use i this case fits in as follows (consider this option #1):

StringBuilder sb = new StringBuilder ();

sb.Append({whatever_other_html_comes_before_the_image});

sb.Append("<img src=\"/files/image1.jpg\" />");

sb.Append({whatever_other_html_comes_after_the_image});
 

StringWriter swr = new StringWriter (sb);

HtmlTextWriter hwr = new HtmlTextWriter (swr);

base.Render (hwr);

string html = sb.ToString ();

writer.Write (html);

Open in new window

0
 
LVL 11

Expert Comment

by:azarc3
ID: 24377971
Consider this option #2:
(Note: this option assumes only one image will be shown and that it has a place marker of {0} where it is supposed to appear. This can be easily extended to a very large number of images using the basis)
StringBuilder sb = new StringBuilder ();

sb.Append(string.format("{whatever_other_html_is_to_be_included}", "/files/image1.jpg");
 

StringWriter swr = new StringWriter (sb);

HtmlTextWriter hwr = new HtmlTextWriter (swr);

base.Render (hwr);

string html = sb.ToString ();

writer.Write (html);

Open in new window

0
 
LVL 11

Accepted Solution

by:
azarc3 earned 500 total points
ID: 24378059
Here's an example of where you would expect many images.
        ArrayList aryImages = new ArrayList();

        aryImages.Add("/files/image1.jpg"); // will be substituted for the "{0}" placeholder

        //... however many you need for your HTML

        aryImages.Add("/files/imageN.jpg"); //matches placeholder "{N}" where N is the count of images.
 

        StringBuilder sb = new StringBuilder();

        sb.Append(string.Format("{whatever_other_html_is_to_be_included}", aryImages.ToArray()));
 

        StringWriter swr = new StringWriter(sb);

        HtmlTextWriter hwr = new HtmlTextWriter(swr);

        base.Render(hwr);

        string html = sb.ToString();

        writer.Write(html);

Open in new window

0
 

Author Closing Comment

by:devnewbee
ID: 31581071
Awesome.  Thanks for the help!
0
 

Author Comment

by:devnewbee
ID: 24378544
Can you elaborate a little more on the placeholder syntax?
0
 
LVL 11

Expert Comment

by:azarc3
ID: 24378637
Yep. It goes in order... so you start at {0} where you want the first subsitution to be. The next will be {1}, and the last will be {N} where N is the count of the items minus 1 (because we started at 0).

You MUST has the same number of items to substitutes as you have placeholders, and vice versa.
0
 

Author Comment

by:devnewbee
ID: 24378691
So, if I have this right, for three images it would be...just want to clarify.

 ArrayList aryImages = new ArrayList();
        aryImages.Add("/files/bg0.jpg"); // will be substituted for the "{0}" placeholder
        //... however many you need for your HTML
        aryImages.Add("/files/image1.jpg"); //matches placeholder "{N}" where N is the count of images.
         aryImages.Add("/files/footer2.jpg"); //matches placeholder "{N}" where N is the count of images.
 

0
 
LVL 11

Expert Comment

by:azarc3
ID: 24402940
That is exactly right. In your composite string that the String Builder will eventually contain needs to include {0}, {1}, and {2}... Otherwise you'll get a runtime error.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now