Solved

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

Posted on 2013-06-20
14
376 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 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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