browse by category or date

The following code will work with the following requirements:

  1. You are using .NET Framework 2.0
  2. You already have WebBrowser control added into your Windows Form
public void DisplayHTML(String strHTML)
{
  if (strHTML != "")
  {         
    if (this.webBrowser2.Document != null)
    {              
      HtmlDocument doc = this.webBrowser2.Document.OpenNew(true);
      doc.Write(strHTML);
    }
    else
    {
      this.webBrowser2.DocumentText = strHTML;
    }               
  }
}

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

I was writing this small program to download a file from Internet. The original code was as follows:

            long content_lenght;
            int buffer_length = 4096;
            int read_count = 0;
            byte[] buffer;
            WebRequest req = WebRequest.Create(
                "http:/images/photobucket/CAP_5288Japan-Posters.jpg");
            WebResponse resp = req.GetResponse();
            FileStream output = new FileStream("output.jpg", FileMode.Create);
            Stream strm = resp.GetResponseStream();
            content_lenght = resp.ContentLength;
            while (read_count < content_lenght)
            {
                if (read_count + buffer_length > content_lenght)
                    buffer = new byte[(int)content_lenght - read_count];
                else
                    buffer = new byte[buffer_length];
                read_count += strm.Read(buffer, 0, buffer.Length);
                output.Write(buffer, 0, buffer.Length);
            }
            strm.Close();
            output.Flush();
            output.Close();

To my dismay, when the program finished its execution, the output file (output.jpg) become a scrambled image. So I thought it could be caused by the buffer size (buffer_length) that is too huge. So I reduced buffer_length from 4096 to 1024. It did worked. But when I run the program for a few more times, suddenly the image become scrambled again. What kind of problem is this?

Then something struck me, apparently I forgot that Internet is an unreliable medium. The bytes available inside the input buffer maybe not be as much as the number that I tried to read. Initially I have this thought that Stream.Read(…) will pause the execution thread waiting for the input buffer to have at least the number of bytes that I requested before allowing the read process to commence. So it seems the fact is contrary to what I think. Or does the waiting process only happen when we only read one single byte? So I modified my program into something like this:

            long content_lenght;
            int read_count = 0;            
            WebRequest req = WebRequest.Create("http:/images/photobucket/CAP_5288Japan-Posters.jpg");
            WebResponse resp = req.GetResponse();
            FileStream output = new FileStream("output.jpg", FileMode.Create);
            Stream strm = resp.GetResponseStream();
            content_lenght = resp.ContentLength;
            byte[] buffer= new byte[content_lenght];
            while (read_count < content_lenght)
            {
                buffer[read_count] = (byte)strm.ReadByte();
                ++read_count;
            }
            output.Write(buffer, 0, buffer.Length);
            strm.Close();
            output.Flush();
            output.Close();

It works like a charm (OOT: The statement is quite oxymoron. I mean, can anyone show me what kind of charm that scientifically proven? Okay, perhaps I should have said 'It works as I expected' 😀 ). Tragedy struck when I found out that there is even a simpler way to achieve above code's purpose:

            WebClient wec = new WebClient();
            wec.DownloadFile("http:/images/photobucket/CAP_5288Japan-Posters.jpg", 
                  "output.jpg");

So what do you think?

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

Today a friend of mine went for vacation to Japan. During lunch yesterday, many of my female colleagues passed him money as they want to buy cosmetics from Japan. These made-in Japan cosmetics are quite popular among the girls. Then he asked me whether I want anything from Japan. Hmm… though question. The only answer I can come up was T-Shirt 🙂

So yeah.. I kept thinking about what kind of things that are interesting, can only be found in Japan, and not cosmetics ? 😀 I mean, what do guys want to buy from Japan?

So preparing for my next colleague (I am yet to found out who he or she is :P) who will go to Japan, I googled for some interesting things to buy from Japan. Ok, let see what we have ..

Japan Art Print by Ignacio
  1. Phallus-shaped foods, candy, key-chain, bookmarker and vagina-shaped sake pitcher. Interesting …. 😛 Too bad the festival is only held every 15 March
  2. If I am an Otaku, I might buy manga, anime and Gundam toys. But I’m not, so it’s a no 🙂
  3. Gadgets? Too bad I am kinda short in cash a cheapo (Russel Peters said, “It’s in your blood man…”). And beside, I have my own way of shopping 🙂

Hmm.. so yes.. I think I am settle with T-Shirt 🙂 Unless you have different opinion?

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.