PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

BinaryWrite GIF from Binary String

 
 
andrew.douglas11@gmail.com
Guest
Posts: n/a
 
      11th Mar 2008
Hello,

I'm looking to display an image in the browser using a binary string
containing all the bytes that make up a GIF image. I've tried all
the standard encodings in System.Text.Encoding, but the images aren't
showing up. I'm using the approach where I'm setting ImageURL =
"GetImage.aspx?id=1". Here's my code from GetImage.aspx:

Dim imageData As String = the binary data representing a .gif file
Dim imageBytes() As Byte =
System.Text.Encoding.ASCII.GetBytes(imageData)

Response.Expires = 0
Response.Buffer = True
Response.Clear()
Response.ContentType = "image/gif"
Response.BinaryWrite(imageBytes)
Response.End()

Can anyone offer any insight as to why this won't work? Using the
ASCII encoding, imageBytes.Length is the same size as
imageData.Length, but all the other standard encodings differ. Also,
if I use ASCII encoding and write the byte array to a file, I noticed
that the file size is very close to, but doesn't match the original
exactly. If I open the 2 different files using Notepad, I see special
characters like the Euro in the working original .gif file, but all
the special characters in the other file show up as a question mark
character.

Any help is greatly appreciated!

Andy
 
Reply With Quote
 
 
 
 
Peter Bromberg [C# MVP]
Guest
Posts: n/a
 
      11th Mar 2008
I'm not exactly sure what your goal is here, string is not necessarily
"binary data".

Here is some code that does work, if it helps:


protected void Page_Load(object sender, EventArgs e)
{
WebClient wc = new WebClient();
string url = "http://www.google.com/intl/en_ALL/images/logo.gif";
byte[] b = wc.DownloadData(url);
Response.Buffer = true;
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite(b);
}

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


"(E-Mail Removed)" wrote:

> Hello,
>
> I'm looking to display an image in the browser using a binary string
> containing all the bytes that make up a GIF image. I've tried all
> the standard encodings in System.Text.Encoding, but the images aren't
> showing up. I'm using the approach where I'm setting ImageURL =
> "GetImage.aspx?id=1". Here's my code from GetImage.aspx:
>
> Dim imageData As String = the binary data representing a .gif file
> Dim imageBytes() As Byte =
> System.Text.Encoding.ASCII.GetBytes(imageData)
>
> Response.Expires = 0
> Response.Buffer = True
> Response.Clear()
> Response.ContentType = "image/gif"
> Response.BinaryWrite(imageBytes)
> Response.End()
>
> Can anyone offer any insight as to why this won't work? Using the
> ASCII encoding, imageBytes.Length is the same size as
> imageData.Length, but all the other standard encodings differ. Also,
> if I use ASCII encoding and write the byte array to a file, I noticed
> that the file size is very close to, but doesn't match the original
> exactly. If I open the 2 different files using Notepad, I see special
> characters like the Euro in the working original .gif file, but all
> the special characters in the other file show up as a question mark
> character.
>
> Any help is greatly appreciated!
>
> Andy
>

 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      11th Mar 2008
you need to know which encoding was used to convert the binary data to a
string. then you use the matching decode. if ascii encoding was used, this is
only 7bit, so data was lost converting to a string (high bit), so there is no
way to get it back on the decode.

-- bruce (sqlwork.com)


"(E-Mail Removed)" wrote:

> Hello,
>
> I'm looking to display an image in the browser using a binary string
> containing all the bytes that make up a GIF image. I've tried all
> the standard encodings in System.Text.Encoding, but the images aren't
> showing up. I'm using the approach where I'm setting ImageURL =
> "GetImage.aspx?id=1". Here's my code from GetImage.aspx:
>
> Dim imageData As String = the binary data representing a .gif file
> Dim imageBytes() As Byte =
> System.Text.Encoding.ASCII.GetBytes(imageData)
>
> Response.Expires = 0
> Response.Buffer = True
> Response.Clear()
> Response.ContentType = "image/gif"
> Response.BinaryWrite(imageBytes)
> Response.End()
>
> Can anyone offer any insight as to why this won't work? Using the
> ASCII encoding, imageBytes.Length is the same size as
> imageData.Length, but all the other standard encodings differ. Also,
> if I use ASCII encoding and write the byte array to a file, I noticed
> that the file size is very close to, but doesn't match the original
> exactly. If I open the 2 different files using Notepad, I see special
> characters like the Euro in the working original .gif file, but all
> the special characters in the other file show up as a question mark
> character.
>
> Any help is greatly appreciated!
>
> Andy
>

 
Reply With Quote
 
andrew.douglas11@gmail.com
Guest
Posts: n/a
 
      12th Mar 2008
Thanks for the replies - how do I find how the encoding? It's
whatever encoding that .Net uses in GDI+ to save a .GIF file. Here's
how the image is created:

'From: http://www.chrisfrazier.net/blog/arc...05/27/276.aspx
Dim stream As New
System.IO.MemoryStream(Convert.FromBase64String(imageData))
Dim noteImage As System.Drawing.Bitmap =
DirectCast(System.Drawing.Image.FromStream(stream),
System.Drawing.Bitmap)
noteImage.Save(fs, System.Drawing.Imaging.ImageFormat.Gif)
stream.Close()

After this code runs, the file is stored to a String field in a Siebel
database (I realize that a different type might be preferable).
However, the .gif file is created correctly in the local file system,
and when I pull the string OUT of the database, it's the exact same
length in bytes as the file.

I've tried all the standard encodings (Unicode, UTF7, UTF8, UTF32,
BigEndianUnicode, and Default), and none of them seem to work. ASCII
gets me the closest to the correct byte size though. Is this a case
where I need to resort to referring to a numbered CodePage?

Thanks!

Andy
 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      12th Mar 2008
(E-Mail Removed) wrote:
> Thanks for the replies - how do I find how the encoding? It's
> whatever encoding that .Net uses in GDI+ to save a .GIF file. Here's
> how the image is created:

<snip>

..Net (and everything else) doesn't use any encoding to save a .gif file -
it's just a load of bytes with no relation to text.

> I've tried all the standard encodings (Unicode, UTF7, UTF8, UTF32,
> BigEndianUnicode, and Default), and none of them seem to work. ASCII
> gets me the closest to the correct byte size though. Is this a case
> where I need to resort to referring to a numbered CodePage?


The chrisfrazier web site is being too slow for me to see a reason for you
wanting to treat binary data as text, but basically you want the system to
use an 8-bit character set. UTF and ASCII are not; Windows-1252 and so on
are.

If the gif file exists on disk, you can use Response.WriteFile().

Andrew


 
Reply With Quote
 
Andrew Douglas
Guest
Posts: n/a
 
      12th Mar 2008
Got it working - thanks everyone for the replies! Bruce was correct
about losing data. The image data was coming from a Base64 String,
and I was losing data going to String. With
System.Convert.ToBase64String(data), all the bytes are as they were
when I saved the .gif.

Thanks all!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change binary data buffer read from a binary file to string format Anderson Microsoft VC .NET 1 21st Jul 2006 11:35 AM
SqlDataAdapter.Update(DataSet, string) -> String or binary data would be truncated. languy Microsoft C# .NET 1 1st Mar 2006 07:44 PM
SqlDataAdapter.Update(DataSet, string) -> String or binary data would be truncated. languy Microsoft C# .NET 0 1st Mar 2006 12:05 PM
BinaryWrite an Image from a binary string =?Utf-8?B?QmFtbWVyMjI=?= Microsoft C# .NET 2 2nd Mar 2004 07:39 PM
Binary String to bytes and/or HEX-string? Kenneth Priisholm Microsoft Dot NET Framework 2 22nd Jul 2003 04:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:47 AM.