Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How convert byte stream to image in asp.net

Posted on 2011-05-10
14
Medium Priority
?
2,932 Views
Last Modified: 2012-05-11
Hi,

 I am using Asp.Net application. I am having a input byte stream like
<ffd8ffe0 00104a46 49460001 01000001 00010000 ffe10058 45786966 00004d4d 002a0000 00080002 01120003 00000001 00010000 87690004 00000001 00000026 00000000 0003a001 00030000 00010001 0000a002 00040000 00010000 002ca003 00040000 00010000 00090000 0000ffdb 00430002 01010101 01020101 01020202 02020403 02020202 05030403 04060506 06060505 05060709 08060708 07050508 0b080809 090a0a0a 06070b0c 0b0a0c09 0a0a0aff db004301 02020202 02020503 03050a06 05060a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a ffc00011 08000900 2c030122 00021101 031101ff c4001f00 00010501 01010101 01000000 00000000 00010203 04050607 08090a0b ffc400b5 10000201 03030204 03050504 04000001 7d010203 00041105 12213141 06135161 07227114 328191a1 082342b1 c11552d1 f0243362 7282090a 16171819 1a252627 28292a34 35363738 393a4344 45464748 494a5354 55565758 595a6364 65666768 696a7374 75767778 797a8384 85868788 898a9293 94959697 98999aa2 a3a4a5a6 a7a8a9aa b2b3b4b5 b6b7b8b9 bac2c3c4 c5c6c7c8 c9cad2d3 d4d5d6d7 d8d9dae1 e2e3e4e5 e6e7e8e9 eaf1f2f3 f4f5f6f7 f8f9faff c4001f01 00030101 01010101 01010100 00000000 00010203 04050607 08090a0b ffc400b5 11000201 02040403 04070504 04000102 77000102 03110405 21310612 41510761 71132232 81081442 91a1b1c1 09233352 f0156272 d10a1624 34e125f1 1718191a 26272829 2a353637 38393a43 44454647 48494a53 54555657 58595a63 64656667 68696a73 74757677 78797a82 83848586 8788898a 92939495 96979899 9aa2a3a4 a5a6a7a8 a9aab2b3 b4b5b6b7 b8b9bac2 c3c4c5c6 c7c8c9ca d2d3d4d5 d6d7d8d9 dae2e3e4 e5e6e7e8 e9eaf2f3 f4f5f6f7 f8f9faff da000c03 01000211 0311003f 00fb0bf6 7ef87be3 6f8bdade 89ab5df8 6f5297c2 9a95fbda ddeb1a63 a486d880 412f8dc6 3dac54e5 d40c107a 1cd7d5ff 000fff00 678f87be 0af0adaf 87bc4924 5ad5c69d af1d4ac3 5230fd9e 553942a8 d863b971 1a86524a b75c038c 7917fc12 c7fe4d9b 51ff00b1 c6efff00 49ad6bdf b52ef5c7 56b4a32b 23dfc065 f46ad352 9752f6a0 fe0f77d5 5e5d0ed1 9b5b8847 ab9f2803 76a10a01 27ae1588 fc6b0b59 b2f871a9 78834af1 16afe1ab 4b89b458 0c1a7232 feea28c9 5c011fdd 3b769db9 1f2ee38e 70452d47 a7e1585a 8f53f535 c92c4d45 b1f43432 6c24f492 3c63e28f ece9e21f 0f68fa45 afc2c5d5 7c4faa5f 5f4d1df4 89124105 ba2aa6c0 7270992e 46e77f9b 69c01835 f3f5d7c5 8fb1dcc9 6935caef 89ca3ec9 438c8383 8604823d c715f6b6 9fff0021 db5ffaf8 5fe75f98 15d985af 2ad17cdd 0f9dcfb2 ba396568 2a6eea4a fe96b1ff d9>

How can i convert this byte stream to an image in C#? It is very urgent.

Hope your reply.
0
Comment
Question by:guvera
14 Comments
 
LVL 11

Expert Comment

by:saragani
ID: 35735470
Is it like this or you have an actual object like Byte Array, or Memory Stream?
0
 
LVL 11

Accepted Solution

by:
saragani earned 1332 total points
ID: 35735490
Hi, in case you have a Byte[] or MemoryStream you can use the following code.
It accepts a Byte[] and makes a MemoryStream from it and loads an Image from that stream.
If you already have a MemoryStream then just make the function get the stream instead of Byte[]

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream img = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(img);
     return returnImage;
}
0
 
LVL 8

Author Comment

by:guvera
ID: 35735505
Hi,

  Thanks for your reply. This is Byte Array.

Thanks
Guvera
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 11

Expert Comment

by:saragani
ID: 35735533
Ok, so you have my solution above.
After you get an Image object, just do like this:

pictureBox1.Image = image;

If you have an <Img> tag then it is a little more complicated, since you will need to save the Image to a file first and then give it as a source to the Img.
0
 
LVL 8

Author Comment

by:guvera
ID: 35735578
Hi Saragani,

 I got a error message in this code.   Image.FromStream(img);

system.web.UI.webcontrols.Image does not contain contain definition for 'FromStream'.

Thanks
Guvera
0
 
LVL 11

Expert Comment

by:saragani
ID: 35735592
Hi, the Image is not a web control. It should be a System.Drawing
0
 
LVL 8

Author Comment

by:guvera
ID: 35736096
Hi,

Thanks for your reply.

 I put the code like,

 MemoryStream ms = new MemoryStream(bytes);
 System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
 return returnImage;

In the second line again i got an exceptiont that parameter is not valid.

Regards
Guvera
0
 
LVL 11

Assisted Solution

by:saragani
saragani earned 1332 total points
ID: 35736141
Hi, please check that your byte array (bytes) contains data.
I googled and I've found this:

http://huntjason.wordpress.com/2007/09/24/system-drawing-image-fromstream-and-the-parameter-is-not-valid-argument-exception-alternate-title-testing-the-reconstitution-of-images-from-byte-arrays/

Another option is that the data is not OK. Maybe the data is not suitable for creating an image from it:
http://stackoverflow.com/questions/629955/parameter-not-valid-exception-loading-system-drawing-image
(The "Parameter is not Valid" is most probably because the data in the byte array is not an "image' format.)
0
 
LVL 11

Expert Comment

by:saragani
ID: 35736323
Where did you get those bytes from?
I see that the bytes starts with ffd8ffe0

If it were bytes that were taken from a file then I would have expected it to have a header that matches some kind of an image type (I would see the ASCII of the letters BM, PNG, JPF etc...).

In this case, I suspect that those are the bytes of the data of the images (excluding the image header with all of it's properties, like width hight, color depth etc).

If that's the case, do you at least know the original width and height of the image that the bytes where taken from?
0
 
LVL 16

Assisted Solution

by:Rose Babu
Rose Babu earned 668 total points
ID: 35736395
Hi,

try the following

Dim ImageData As Byte() = {<your-byte-data>}

Dim ms As New MemoryStream

ms = New System.IO.MemoryStream(ImageData)

PictureBox1.Image = Image.FromStream(ms)

Open in new window


Hop you will get the answere you want.

Happy Programming
0
 
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 35736430
Hi
if you are storing your image in database and the field type is image
then do the following
in ur page add <asp:image .. control
and in code behind set the image path to the following

 Image1_P.ImageUrl = "../imageSource.aspx
where you have to create this page imageSource.aspx  in ur project and do modify anything inside it
now in this page code behind do the following

 
DataTable dt = new DataTable();

dt = GetMyBytePicture();

int c = dt.Rows.Count;

if (c > 0) {

	Response.ContentType = "image/jpeg";
	Response.Expires = 0;
	Response.Buffer = true;
	Response.Clear();
	Response.BinaryWrite(Convert.ToByte(dt.Rows[0]["Picture"]));
	HttpContext.Current.ApplicationInstance.CompleteRequest();


}

Open in new window

0
 
LVL 11

Expert Comment

by:saragani
ID: 35737319
Hi, any answers to my questions above?
0
 
LVL 8

Author Comment

by:guvera
ID: 35737777
Hi Saragani,

  Actually i am not getting the solution. I will save the stream as a bin file format and need to convert it.

 Thanks
Guvera
0
 
LVL 11

Expert Comment

by:saragani
ID: 35738319
Hi, you still didn't answer my question. Where do you get the bytes from?
It doesn't seem to be a valid image bytes.

You wrote that you will need to convers it... The problem is how and to what?!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

577 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