Solved

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

Posted on 2013-06-20
14
380 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

710 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