Solved

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

Posted on 2013-06-20
14
374 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 M earned 167 total points
Comment Utility
0
 

Author Comment

by:VBBRett
Comment Utility
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 M
Comment Utility
What do you mean assign it to an image?
0
 
LVL 74

Expert Comment

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

Author Comment

by:VBBRett
Comment Utility
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
Comment Utility
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 M
Comment Utility
Is it always guaranteed to be an image file ?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

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

Expert Comment

by:Ron M
Comment Utility
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
Comment Utility
I really didn't want to save the image to disc...I wanted to stream it from memory.  Is that possible?
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
Comment Utility
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
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
Comment Utility
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
Comment Utility
Thank you.  Will try the code now.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now