473,669 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BinaryWrite GIF from Binary String

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.Enc oding, 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.Enc oding.ASCII.Get Bytes(imageData )

Response.Expire s = 0
Response.Buffer = True
Response.Clear( )
Response.Conten tType = "image/gif"
Response.Binary Write(imageByte s)
Response.End()

Can anyone offer any insight as to why this won't work? Using the
ASCII encoding, imageBytes.Leng th is the same size as
imageData.Lengt h, 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
Mar 11 '08 #1
5 3399
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(objec t 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.Conten tType = "image/gif";
Response.Binary Write(b);
}

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
"an************ **@gmail.com" 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.Enc oding, 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.Enc oding.ASCII.Get Bytes(imageData )

Response.Expire s = 0
Response.Buffer = True
Response.Clear( )
Response.Conten tType = "image/gif"
Response.Binary Write(imageByte s)
Response.End()

Can anyone offer any insight as to why this won't work? Using the
ASCII encoding, imageBytes.Leng th is the same size as
imageData.Lengt h, 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
Mar 11 '08 #2
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)
"an************ **@gmail.com" 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.Enc oding, 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.Enc oding.ASCII.Get Bytes(imageData )

Response.Expire s = 0
Response.Buffer = True
Response.Clear( )
Response.Conten tType = "image/gif"
Response.Binary Write(imageByte s)
Response.End()

Can anyone offer any insight as to why this won't work? Using the
ASCII encoding, imageBytes.Leng th is the same size as
imageData.Lengt h, 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
Mar 11 '08 #3
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.Memor yStream(Convert .FromBase64Stri ng(imageData))
Dim noteImage As System.Drawing. Bitmap =
DirectCast(Syst em.Drawing.Imag e.FromStream(st ream),
System.Drawing. Bitmap)
noteImage.Save( fs, System.Drawing. Imaging.ImageFo rmat.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,
BigEndianUnicod e, 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
Mar 12 '08 #4
an************* *@gmail.com 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,
BigEndianUnicod e, 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.WriteF ile().

Andrew
Mar 12 '08 #5
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!
Mar 12 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3884
by: Kevin Humphreys | last post by:
Hi, How can I output the Response.BinaryWrite content in an asp page that has html tags inside? I need to write the binary data to the client browser inside the html tags. Thanks In Advance, Kevin.
1
3787
by: Kevin Humphreys | last post by:
Hi All, Please help me regarding the 'Response.BinaryWrite' I am making one web application where I need to store some of client logo's and others images. In this context I am able to store images into .DB file and even able to display into browser to. But it seems to be 'Response.BinaryWrite' does not support 'html/text' hearder if you user 'BinaryWrite'? I had tried many way to do so but its does not write binary if pages content...
0
2556
by: Bill | last post by:
I'm using the example shown at : http://support.microsoft.com:80/support/kb/articles/q276/4/88.asp It involves using ASP/VbScript and ADODB.Stream to read binary data from a *.DOC or *.RTF file and then send it to the user with Response.ContentType and BinaryWrite. Everything works fine until I try to Replace() certain strings in the *.RTF file with my own data. I'm assuming Replace() is choking on the binary characters, or
5
17879
by: katrinaVictim | last post by:
Question: I get the eror listed at the bottom of the post. What can I do to make the response of the x1.send a "binary" type? Or, in general, how can I just "make this work"? <%@ Language=VBScript %> <% Response.Buffer = TRUE Response.ContentType = "image/jpg"
2
6131
by: Bammer22 | last post by:
I am trying to BinaryWrite an image from a binary string that I am storing in the web.config file. The image is just a 1x1 pixel gif. I am setting the content type to "image/gif", but the output is just my string. Any suggestions? Code is below byte bytes = Encoding.UTF8.GetBytes(ConfigurationSettings.AppSettings.ToString()) httpContext.Response.ContentType = "image/gif"; httpContext.Response.BinaryWrite(bytes)
2
6078
by: Nik | last post by:
I am trying to write out the equivalent of this asp statement in c#: Response.BinaryWrite(chrb(239) & chrb(187) & chrb(191)) 'BOM = EF BB BF string binString = "11111111"; byte myBin = Convert.ToByte(binString); Response.BinaryWrite(myBin); binString = "10111011"; myBin = Convert.ToByte(binString); Response.BinaryWrite(myBin); binString = "10111111";
1
2399
by: abcd | last post by:
I am writing a page which uses response.binarywrite to the client, after it has completed sending the file I am interested in I want to display some mesange like "download completed" etc...I am unable to set such javascript ..... my code Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name Response.AddHeader "Content-Length", objFile.Size
5
12063
by: twiggy182 | last post by:
Hi, I really need you help because I'm not very familliar with ASP and I could not find any solution to my problem. To put you in situation, I have a CGI to which I send a file name, and that script return me that file. But for security reason, I don't want to publish the address of this CGI, so I encapsulated it in an ASP file. This way, only this ASP file knows where the CGI is.
1
4214
by: mattridings | last post by:
Hi gang, Have a script that works fine. However, it's really cpu intensive and I'm looking for suggestions on a) whether or not that's normal and if so b)a better way of doing it. Script is very simple, there is a folder of files with numeric names. The real file name is stored in the database. User clicks on link that executes script and provides database record id. Record is looked up, file name is found, and browser is delivered...
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.