Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-06-20
14
Medium Priority
?
384 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
  • 7
  • 4
  • 3
14 Comments
 
LVL 25

Accepted Solution

by:
Ron Malmstead earned 668 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1332 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 1332 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

916 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