Solved

How to stream a byte[] array into a file or image

Posted on 2013-06-20
14
381 Views
Last Modified: 2013-10-04
How do you stream a byte array into a file or image.  I do not want the image to be a physical file, but a file stream via memory.  How can this be done?
0
Comment
Question by:VBBRett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 25

Accepted Solution

by:
Ron Malmstead earned 167 total points
ID: 39263704
0
 

Author Comment

by:VBBRett
ID: 39263718
I have the following code that should do the trick, but how do you assign the method to an image?

 private Image byteArrayToImage(byte[] byteArrayIn)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn);
            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

            return returnImage;
        }

Open in new window

0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39263745
What do you mean assign it to an image?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39263812
What is the business goal here?
0
 

Author Comment

by:VBBRett
ID: 39264061
Oh, basically how would I assign the Image method to an image?  basically, here is an example of what I am talking about...it is not real code, just pseudocode:

byte[] content = (byte[])ProfileImage;

Image.FromImage = byteArrayToImage(content);

I hope that explains what I am talking about.
0
 

Author Comment

by:VBBRett
ID: 39264064
So basically I want the end goal to be the Image object to be filled by the method.
0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39264186
Is it always guaranteed to be an image file ?
0
 

Author Comment

by:VBBRett
ID: 39264228
Yes...it is always guaranteed to be an image file.
0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39264269
Clearly you already have the image byte array in memory.. so the links I provided in my first comment, will show you how to take that array and save it to a file on disk.  Essentially the only thing you need to do, is to put the image file name + extension, in the sample code, and provide a path on the server to save it.  Your code looks fine to me.. the only thing missing that I can see, is saving it to disk.

The only other issue you might run into, is different image types..(bmp, jpeg,tiff etc etc.)
In which case, if the file type can't be "guaranteed", then you would need to do some inspecting of the first 8 bytes of the file bytes.. to get the extension., which is a different question entirely.
0
 

Author Comment

by:VBBRett
ID: 39264424
I really didn't want to save the image to disc...I wanted to stream it from memory.  Is that possible?
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
ID: 39265867
I must be misunderstanding something. Based on what I read, you should simply have to do:

byte[] content = (byte[])ProfileImage;
Image i = byteArrayToImage(content);

Open in new window

0
 

Author Comment

by:VBBRett
ID: 39266982
That is not what I meant, I am asking how to assign it to an image that is already on your web page.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
ID: 39268228
You would have to serve it as a resource from your site. What I mean by that is that you must give a URL within the page that points to this image. How you store it on the server is up to you. You can write it out to disk in one of your site's folders, or you can place the file into session, and then create another "page" that only exists to serve up images from session. This new "page" is the resource your webpage will be seeking; it just so happens that this page returns an image rather than an actual web page.

e.g.

Main Page Markup
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_28163161._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    Some regular content.
    <img src="GetImage.aspx" />
</asp:Content>

Open in new window


Main Page Code-Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _28163161
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // you logic

            Session["image"] = PofileImage;

            // any other logic
        }
    }
}

Open in new window


Image Page Markup
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetImage.aspx.cs" Inherits="_28163161.GetImage" %>

Open in new window


Image Page Code-Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _28163161
{
    public partial class GetImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Headers.Add("Content-Type", "img/png");
            Response.BinaryWrite((byte[])Session["image"]);
        }
    }
}

Open in new window

0
 

Author Comment

by:VBBRett
ID: 39325622
Thank you.  Will try the code now.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question