Need to convert bytes to image in web page ASP.Net C#

I am utilizing a web services that delivers me an image in an array of bytes.  I am having no issues retrieving the bytes, however, I can't figure out how to convert these bytes into a System.Web.UI.WebControls.Image.  I just want a clearcut answer where I'm starting with the bytes themselves.  I have no need to upload from any file or database, or to save back to one, I just want to take the bytes I already have onhand, convert them to an image, and display them in the page.  

If anyone could give a clear example from start to finish I'd appreciate it, thanks.
RinaXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wht1986Commented:
I've always used an ashx handler but i imagine an asmx should not be much harder to implement.

<img src='http://www.site.com/yourservice.asmx/yourmethod?someImageParameter=12' />

Just make sure the web service can accept http GET, not just post
0
wht1986Commented:
add this to your web.config to turn on httpget and post

<configuration>
    <system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
    </system.web>
</configuration>
0
RinaXAuthor Commented:
Let me be clearer.  I have the bytes already saved in a byte[] array variable, imageBytes.  I need to then take those bytes and convert them to an image. The only thing the web service does is deliver the image as bytes.  
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

wht1986Commented:
I understood that, if the bytes being returned are the bytes of the image, then setting the src of your html img element or webcontrol image will automatically display those bytes as an image.

I typically create a handler called Image.ashx which takes a querystring parameter to say what image to return. then on the web page I just have to say

<asp:Image ID="Image1" runat="server" ImageUrl="Image.ashx?ImageNumber=3" />

Here's an example of the handler
http://www.eggheadcafe.com/articles/20041104.asp

The most important part is setting of the content type
Response.ContentType = "image/Jpeg";
Response.BinaryWrite( yourBytes[] );

So what I am saying is if you are using a web service, you can call a particular method of the asmx by setting the URL appropriately as above to obtain the same results.  If you want I can work on a working example when I get home this evening.
0
wht1986Commented:
I did a quick example.  Picture 1 below shows I have 2 images in a folder.  Picture 2 below shows the web page displaying the images. The Wem Service and aspx page are included in the code snippet below
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
    public WebService() { }
 
    [WebMethod]
    public byte[] GetImage(string filename)
    {
 
        byte[] rawData = File.ReadAllBytes(Server.MapPath("images\\" + filename));
        HttpContext.Current.Response.ContentType = "image/Jpeg";
        HttpContext.Current.Response.BinaryWrite(rawData);
        HttpContext.Current.Response.End();
        return rawData;
    }
}
 
===================
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Image ID="Image1" runat="server" ImageUrl="WebService.asmx/GetImage?filename=1.jpg" />
        <asp:Image ID="Image2" runat="server" ImageUrl="WebService.asmx/GetImage?filename=2.jpg" />
    </form>
</body>
</html>

Open in new window

Capture.JPG
Capture1.JPG
0
wht1986Commented:
oops sorry meant to take out the return as we dont really need it
    [WebMethod]
    public void GetImage(string filename)
    {
 
        byte[] rawData = File.ReadAllBytes(Server.MapPath("images\\" + filename));
        HttpContext.Current.Response.ContentType = "image/Jpeg";
        HttpContext.Current.Response.BinaryWrite(rawData);
        HttpContext.Current.Response.End();
    }

Open in new window

0
techprocessCommented:
<asp:Image id="img1" src="View.aspx" runat="server"/>

and on View.aspx.cs write on page load function...
protected void Page_Load(object sender, EventArgs e)
      {
byte[] Array= fnGetByteArrayFromWebService();// get it from ur web service .
Response.ContentType="image/Jpeg";
Response.BinaryWrite(Array);
Response.Flush();
Response.End();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RinaXAuthor Commented:
Okay, I don't link to the asmx page at all in my app, I go through the WSDL.  This is an external web service that our imaging department is letting us use to retrieve images of specific documents when a clerk enters the number for a specific document.  So I send a soap request and get a reply that is in bytes.  So the image is nowhere on my own drive or in any of my folders.  

Example:  

GetDocument doc = new Document();  <-- the main object
DocRequest req = new Request; <--object to send request parameters

encodedReply reply = doc.GetEncodedDoc(req); <-- If successful parameter returned is in bytes
Bytes[] imageBytes = new byte[60000];

imageBytes = reply.Results.imageDocument <---bytes of image

Right here is where I need to start.  I've implemented the solution via the separate aspx page, but nothing happens:  

I created a new web page ImageGenerator.aspx
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Image"] != null)
        {
            GetImage();
        }
    }

    public void GetImage()
    {
        byte[] ByteArrContent = (byte[])Session["Image"];
        //Response.OutputStream.Write(ByteArrContent, 0, ByteArrContent.Length);  
        Response.ContentType = "image/tiff";
        Response.BinaryWrite(ByteArrContent);
        Response.Flush();
        Response.End();
    }

And added this line in my main page that's making the call:  

<asp:Image ID="clmImg" ImageUrl="ImageGenerator.aspx" runat="server" />

Also, the call to the web service is initiated when the user presses a button.  I don't know if that will make a difference.  
0
wht1986Commented:
Ok if the web service is completely outside your realm of control, then you need to do a hybrid approach like  techprocess showed.  Basically create an additional aspx page that gets the bytes from the webservice and then streams that back to the browser.  Attached is a screenshot of my solution where i have an external web service that returns a stream of bytes of an image. The image chosen in based on an integer value passed to the web service.


======================================
The main page
======================================
 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Image ID="Image1" ImageUrl="Image.aspx?ID=1" runat="server" />
    </form>
</body>
</html>
 
======================================
Ihe page that handles streaming back the image
======================================
// Image.aspx.cs
 
public partial class Image : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int imageID = int.Parse(Request.QueryString["ID"]);
        ImageService.WebService svc = new ImageService.WebService();
        byte[] imagedata = svc.GetImage(imageID);
        Response.ContentType = "image/Jpeg";
        Response.BinaryWrite(imagedata);
        Response.End();
    }
}

Open in new window

Capture.JPG
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.