Invalid character in a Base-64 string.

A

Anton

hi

asp.net 3.5

The code below gives a "Invalid character in a Base-64 string." on the
this code: "byte[] b = Convert.FromBase64String(content);"

The value I've sent into saveURLToImage has been http://www.vg.no

using System.Net;
using System.IO;
using System.Text;
private void saveURLToImage(string url)
{
if (!string.IsNullOrEmpty(url))
{
string content = "";
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();
StreamReader sr = new
StreamReader(webResponse.GetResponseStream(),
Encoding.GetEncoding("UTF-8"));
content = sr.ReadToEnd();
//save to file
byte[] b = Convert.FromBase64String(content);
MemoryStream ms = new MemoryStream(b);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
img.Save(@"c:\pic.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
ms.Close();
}
}

any ideas what I do wrong?
 
H

Hans Kesting

Anton has brought this to us :
hi

asp.net 3.5

The code below gives a "Invalid character in a Base-64 string." on the
this code: "byte[] b = Convert.FromBase64String(content);"

The value I've sent into saveURLToImage has been http://www.vg.no

using System.Net;
using System.IO;
using System.Text;
private void saveURLToImage(string url)
{
if (!string.IsNullOrEmpty(url))
{
string content = "";
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();
StreamReader sr = new
StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
content = sr.ReadToEnd();
//save to file
byte[] b = Convert.FromBase64String(content);
MemoryStream ms = new MemoryStream(b);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
img.Save(@"c:\pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
ms.Close();
}
}

any ideas what I do wrong?

If you print out that "content", does it look like a valid
base64-encoded string? Or are you including http headers and such?

By the way, if you are sure that the contents will be a jpeg image, you
can save the byte[] directly instead of going through Image.

Hans Kesting
 
A

Anton

Hans Kesting said:
Anton has brought this to us :
hi

asp.net 3.5

The code below gives a "Invalid character in a Base-64 string." on the
this code: "byte[] b = Convert.FromBase64String(content);"

The value I've sent into saveURLToImage has been http://www.vg.no

using System.Net;
using System.IO;
using System.Text;
private void saveURLToImage(string url)
{
if (!string.IsNullOrEmpty(url))
{
string content = "";
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();
StreamReader sr = new
StreamReader(webResponse.GetResponseStream(),
Encoding.GetEncoding("UTF-8"));
content = sr.ReadToEnd();
//save to file
byte[] b = Convert.FromBase64String(content);
MemoryStream ms = new MemoryStream(b);
System.Drawing.Image img =
System.Drawing.Image.FromStream(ms);
img.Save(@"c:\pic.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
ms.Close();
}
}

any ideas what I do wrong?

If you print out that "content", does it look like a valid base64-encoded
string? Or are you including http headers and such?

By the way, if you are sure that the contents will be a jpeg image, you
can save the byte[] directly instead of going through Image.

Hans Kesting

I'm not including any http headers that I know...

I don't know how a base64-coded string should look like, but the result
generated looks like this, Below I pasted part of the contens (couldn't
paste the entire contens because it is too large) of the content
variable.

content "\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01
Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">\n\n<html>\n<head>\n<title>VG
Nett - Forsiden</title>\n\n<!-- meta -->\n<meta http-equiv=\"Content-Type\"
content=\"text/html; charset=utf-8\" />\n<meta name=\"description\"
content=\"Norges st&oslash;rste nettsted. Oppdateres minutt for minutt
p&aring; siste nytt innen sport, innenriks, utenriks, og underholdning.\"
/>\n<meta http-equiv=\"refresh\" content=\"600\" />\n<meta
name=\"verify-v1\" content=\"oMgZtqMwDTFfakZF0gC07RcpEoQ5tDpCEzZQ/w7L04Q=\"
/>\n\n<!-- css -->\n<link rel=\"stylesheet\" type=\"text/css\"
href=\"http://static.vg.no/css/hovedstyle.css\" />\n<link rel=\"stylesheet\"
type=\"text/css\" href=\"http://static.vg.no/css/annonser.css\" />\n<link
rel=\"stylesheet\" type=\"text/css\"
href=\"http://www.vg.no/drfront/hovedfront/css/front-25.css\" />\n<link
-landinger \"
href=\"http://go.vg.no/cgi-bin/go.cgi/news...://www.vg.no/reise/artikkel.php?artid=566255\">Les
mer &raquo;</a></p>\n<ul>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...://www.vg.no/reise/artikkel.php?artid=556348\">Togloffer
etter solen </a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...://www.vg.no/reise/artikkel.php?artid=556342\">Ryggsekk
+ tog = pop igjen </a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...://www.vg.no/reise/artikkel.php?artid=556512\">Surfelykke
p&aring; Spanias s&oslash;rspiss </a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...://www.vg.no/reise/artikkel.php?artid=556519\">Spennende
grottesafari i Nordland </a></li>\n</ul>\n</div><div
class=\"helse\">\n<h3><a title=\"G&aring; til Helse-fronten\"
href=\"http://go.vg.no/cgi-bin/go.cgi/newsflash/http://www.vg.no/helse\">Helse</a></h3>\n<h4><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...lse/svineinfluensa/artikkel.php?artid=566263\">Astmatiske
barn kan bli nektet &aring; g&aring; p&aring;
skolen</a></h4>\n<p>HELSEDIREKTORATET (VG Nett) 20 prosent av norske barn
har en form for astma. I verste fall kan mange...</p>\n<p
class=\"leshele\"><a title=\"Les hele Astmatiske barn kan bli nektet &aring;
g&aring; p&aring; skolen\"
href=\"http://go.vg.no/cgi-bin/go.cgi/news...lse/svineinfluensa/artikkel.php?artid=566263\">Les
mer &raquo;</a></p>\n<ul>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...://www.vg.no/helse/artikkel.php?artid=566240\">For
late til &aring; l&oslash;pe etter bussen </a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...://www.vg.no/helse/artikkel.php?artid=566226\">Kl&oslash;e
er ikke smerte </a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/newsflash/http://e24.no/boers-og-finans/article3209925.ece\">Hydro
låner 1 milliard</a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/newsflash/http://e24.no/utenriks/article3209845.ece\">Dansk
verftsgigant stenger dørene</a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/newsflash/http://e24.no/boers-og-finans/article3209836.ece\">-
Nå er tidspunktet for å kjøpe Rec</a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news.../e24-kommentar/orjasaeter/article3209829.ece\">Sykelønnsordningen
er for god</a></li>\n</ul>\n</div><div class=\"teknologi\">\n<h3><a
title=\"G&aring; til Teknologi-fronten\"
href=\"http://go.vg.no/cgi-bin/go.cgi/newsflash/http://www.vg.no/teknologi\">Teknologi</a></h3>\n<h4><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...ww.vg.no/teknologi/artikkel.php?artid=566254\">Spotifys
genistrek er avsl&oslash;rt </a></h4>\n<p>Musikktjenesten ga bort 20 prosent
av aksjene i bytte mot plateselskapenes arkiver. ...</p>\n<p
class=\"leshele\"><a title=\"Les hele Spotifys genistrek er avsl&oslash;rt
\"
href=\"http://go.vg.no/cgi-bin/go.cgi/news...ww.vg.no/teknologi/artikkel.php?artid=566254\">Les
mer &raquo;</a></p>\n<ul>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...ww.vg.no/teknologi/artikkel.php?artid=566222\">HD-krigen
blusser opp p&aring; ny </a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...//www1.vg.no/spill/artikkel.php?artid=566179\">Spillanmeldelse:
Godkjent slapstick-krig </a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...//www1.vg.no/spill/artikkel.php?artid=566180\">Spillanmeldelse:
Suveren svensk slagmark </a></li>\n<li><a
href=\"http://go.vg.no/cgi-bin/go.cgi/news...ww.vg.no/teknologi/artikkel.php?artid=556387\">Disse
kompaktkameraene t&aring;ler sj&oslash;spr&oslash;yt!
</a></li>\n</ul>\n</div><div class=\"bildespes floatfix\">\n<h3><a
title=\"Se flere bildespesialer\"
href=\"http://www.vg.no/bildespesial/spesial.php\">Bildespesialer</a></h3>\n<ul>\n<li><a
href=\"http://www.vg.no/bildespesial/spesial.php?id=7010\"
title=\"Fotballbråk i Molde\"><img
src=\"http://static.vg.no/bildespesial/7010/1249902997.43219_thumb.jpg\"
alt=\"\" /><span>Fotballbråk i Molde</span></a></li>\n<li><a
href=\"http://www.vg.no/bildespesial/spesial.php?id=7009\" title=\"Se
stjernene på Teen Choice Awards\"><img
src=\"http://static.vg.no/bildespesial/7009/1249887457.42657_thumb.jpg\"
alt=\"\" /><span>Se stjernene på Teen Choice Awards</span></a></li>\n<li><a
href=\"http://www.vg.no/bildespesial/spesial.php?id=7008\" title=\"Disse
kjemper om å bli Miss Universe 09\"><img
src=\"http://static.vg.no/bildespesial/7008/1249839558.67503_thumb.jpg\"
alt=\"\" /><span>Disse kjemper om å bli Miss Universe
09</span></a></li>\n</ul>\n</div></div> <!-- /newsflash -->\n\n<div
id=\"footer\">\n<ul>\n<li>[ <a
href=\"http://go.vg.no/cgi-bin/go.cgi/r-b3/http://www.vg.no/nyheter/\">Nyheter</a>
]</li>\n<li>[ <a
href=\"http://go.vg.no/cgi-bin/go.cgi/r-b3/http://www.vg.no/sport/\">Sport</a>
]</li>\n<li>[ <a
href=\"http://go.vg.no/cgi-bin/go.cgi/r-b3/http://www.vg.no/sport/fotball\">Fotball</a>
]</li>\n<li>[ <a
href=\"http://go.vg.no/cgi-bin/go.cgi/r-b3/http://www.vg.no/rampelys/\">Rampelys</a>
]</li>\n<li>[ <a
href=\"http://go.vg.no/cgi-bin/go.cgi/r-b3/http://lista.vg.no/\">VG-lista</a>
]</li>\n<li>[ <a
href=\"http://go.vg.no/cgi-bin/go.cgi/r-b3/http://www.vg.no/soek/\">S&oslash;k</a>
]</li>\n</ul>\n\n<p>Har du henvendelser om denne tjenesten? Ta kontakt med
v&aring;r <script
type=\"text/javascript\">mt('119,101,98,109,97,115,116,101,114,64,118,103,46,110,111',
'vakthavende journalist')</script>.</p>\n<p>Ansvarlig redakt&oslash;r:
<script type=\"text/javascript\">mt('69, 115, 112, 101, 110, 46, 69, 103,
105, 108, 46, 72, 97, 110, 115, 101, 110, 64, 118, 103, 46, 110, 111',
'Espen Egil Hansen');</script>\n / Administrerende direkt&oslash;r: <script
type=\"text/javascript\">mt('106,111,46,99,104,114,105,115,116,105,97,110,46,111,116,101,114,104,97,108,115,64,118,103,46,110,111',
'Jo Christian Oterhals');</script></p>\n<p>Sentralbord VG: 22 00 00
00</p>\n<p class=\"tips\"><strong>TIPS OSS! </strong> SMS til
<strong>2200</strong> &middot; MMS m/bilde til <strong>2200</strong>
&middot; Ring <strong>22 00 00 00</strong> &middot; E-post til <script
type=\"text/javascript\">mt('50,50,48,48,64,118,103,46,110,111',
'tusentipset')</script> </p>\n<p><em>VG Nett har ikke ansvar for innhold
p&aring; eksterne nettsider som det lenkes til.</em></p>\n<p>Kopiering av
materiale fra VG Nett for bruk annet sted er ikke tillatt uten avtale | <a
href=\"http://www.vg.no/copyright.hbs\">&copy; 2009 Verdens Gang
AS</a></p>\n</div> <!-- /footer -->\n</div> <!-- /right_column -->\n\n<div
id=\"extra\">\n<div class=\"rightad\">\n<script language=\"javascript\"
type=\"text/javascript\">\nOAS_AD('x09');\n</script>\n</div> <!--
/rightad -->\n\n<div class=\"rightad\">\n<script language=\"javascript\"
type=\"text/javascript\">\nOAS_AD('x07');\n</script>\n</div> <!--
/rightad -->\n\n\n</div> <!-- /extra -->\n</div> <!--
/wrapper -->\n\n<script type=\"text/javascript\">\nvar seksjon =
\"Front\";\ngetTMqs('http', 'www.vg.no/'+seksjon, '', 'vg_no', 'no',
'ISO-8859-15');\n</script>\n\n<noscript><img
src=\"http://statistik-gallup.net/V11*www.vg.no/Front**vg_no/no/ISO-8859-15//\"
alt=\"\" /></noscript>\n\n<script type=\"text/javascript\">\n// set the
active tab in \"siste nytt\"\nvar sisteNytt_activeTab =
'sistenytt_alle';\n\n// function to swap tabs\nfunction swapSisteNyttTab(to)
{\n if (sisteNytt_activeTab == to) {\n return false;\n }\n\n
jQuery(\"#\" + sisteNytt_activeTab + \", #\" +
to).toggleClass(\"hidden\");\n sisteNytt_activeTab = to;\n\n return
false;\n}\n\n// add the hidden class to some of the \"sistenytt\" boxes and
attach events to the tabs\njQuery(\"#sistenytt_nyheter, #sistenytt_sport,
#sistenytt_rampelys\").toggleClass(\"hidden\");\n\njQuery(\"#sisteNyttAlleTab\").click(function
() { return
swapSisteNyttTab('sistenytt_alle'); });\njQuery(\"#sisteNyttNyheterTab\").click(function
() { return
swapSisteNyttTab('sistenytt_nyheter'); });\njQuery(\"#sisteNyttSportTab\").click(function
() { return
swapSisteNyttTab('sistenytt_sport'); });\njQuery(\"#sisteNyttRampelysTab\").click(function
() { return
swapSisteNyttTab('sistenytt_rampelys'); });\n\njQuery(\"#vghovedmeny
..menuSection\").click(function() {\n var listElem =
jQuery(this).next('ol');\n\n if (listElem.css('display') == 'none') {\n
listElem.css('display', 'block');\n } else {\n
listElem.css('display', 'none');\n }\n\n
jQuery(this).children('a').toggleClass('aktiv');\n\n return
false;\n});\n\n// Toggle the arrow in the active section in the
menu\njQuery('#vghovedmeny
ol.aktiv').prev().children('a').toggleClass('aktiv');\n\n// Show first
element straight away and mark it as the active
entry\njQuery('#vgHurraEntries
li:first').show().toggleClass('activeEntry');\n\nsetInterval(function() {\n
// Fetch current and next\n var currentEntry = jQuery(\"#vgHurraEntries
li.activeEntry\");\n var nextEntry = currentEntry.next();\n\n // No
more elements? Start at the first\n if (!nextEntry.length) {\n
nextEntry = jQuery(\"#vgHurraEntries li:first\");\n }\n\n
currentEntry.fadeOut('slow').toggleClass('activeEntry');\n
nextEntry.fadeIn('slow').toggleClass('activeEntry');\n},
5000);\n</script>\n<script type=\"text/javascript\">\n<!--\nxtnv = document;
//parent.document or top.document or document \nxtsd =
\"http://logi163\";\nxtsite = \"417915\";\nxtn2 = \"1\"; // level 2
site \nxtpage = \"Front\"; //page name (with the use of :: to create
chapters)\nxtdi = \"\"; //implication
degree\n//-->\n</script>\n<script type=\"text/javascript\"
src=\"http://static.vg.no/xiti/xtcore_vgnett.js\"></script>\n<noscript>\n<img
width=\"1\" height=\"1\" alt=\"\"
src=\"http://logi163.xiti.com/hit.xiti?s=417915&s2=1&p=Front&di=&\"
 
H

Hans Kesting

It happens that Anton formulated :
Hans Kesting said:
Anton has brought this to us :
hi

asp.net 3.5

The code below gives a "Invalid character in a Base-64 string." on the
this code: "byte[] b = Convert.FromBase64String(content);"

The value I've sent into saveURLToImage has been http://www.vg.no

using System.Net;
using System.IO;
using System.Text;
private void saveURLToImage(string url)
{
if (!string.IsNullOrEmpty(url))
{
string content = "";
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();
StreamReader sr = new
StreamReader(webResponse.GetResponseStream(),
Encoding.GetEncoding("UTF-8"));
content = sr.ReadToEnd();
//save to file
byte[] b = Convert.FromBase64String(content);
MemoryStream ms = new MemoryStream(b);
System.Drawing.Image img =
System.Drawing.Image.FromStream(ms);
img.Save(@"c:\pic.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
ms.Close();
}
}

any ideas what I do wrong?

If you print out that "content", does it look like a valid base64-encoded
string? Or are you including http headers and such?

By the way, if you are sure that the contents will be a jpeg image, you
can save the byte[] directly instead of going through Image.

Hans Kesting

I'm not including any http headers that I know...

I don't know how a base64-coded string should look like, but the result
generated looks like this, Below I pasted part of the contens (couldn't paste
the entire contens because it is too large) of the content
variable.

content "\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01
Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">\n\n<html>\n<head>\n<title>VG
Nett - Forsiden</title>\n\n<!-- meta -->\n<meta http-equiv=\"Content-Type\"
content=\"text/html; charset=utf-8\" />\n<meta name=\"description\"
content=\"Norges st&oslash;rste nettsted. Oppdateres minutt for minutt
p&aring; siste nytt innen sport, innenriks, utenriks, og underholdning.\"

That is your problem: this is an html page, NOT a base64 encoded image.

Base64 is an encoding to allow binary contents to be passed through a
medium that only allows (ASCII) characters.
See this snippet:

public class MyClass
{
public static void Main()
{
string s = "this is an example!";
byte[] ba = System.Text.Encoding.UTF8.GetBytes(s);
string b64 = Convert.ToBase64String(ba);
Console.WriteLine(b64);
Console.ReadKey();
}
}

What are you trying to do?

Hans Kesting
 
A

Anton

I'm trying to generate an image based on the contents of an webpage, in
other words: make a snapshot of the webpage and store it on the server. So
then I'm trying this code to see if it can help me with that. Not sure this
is the best approach.
 
H

Hans Kesting

Anton formulated on maandag :
I'm trying to generate an image based on the contents of an webpage, in other
words: make a snapshot of the webpage and store it on the server. So then I'm
trying this code to see if it can help me with that. Not sure this is the
best approach.

You can't do it by just dumping the html code in an image, as you found
out. As I said, the Base64 encoding is just a way to send any list of
bytes through a system that only onderstands ASCII. It will not
generate an image from html text supplied.

To do that, you will need some browser component that renders the html
supplied. There is an AxWebBrowser component. Maybe you can use that?
(I have no experience with it).

see for instance:
http://www.codeproject.com/KB/cs/webbrowser.aspx

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top