Solved

How can I show images using stringbuilder and htmltextwriter?

Posted on 2009-05-13
15
2,978 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net web application 3 44
asp.net uploading picture to a temporary folder 4 25
Amazon S3 Images with .Net 3 29
Error building VS2105 solution from repository 1 33
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

735 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