Click here to Skip to main content
12,819,913 members (33,271 online)
Click here to Skip to main content
Add your own
alternative version

Stats

1.1M views
20.4K downloads
186 bookmarked
Posted 3 Sep 2006

C# Image to Byte Array and Byte Array to Image Converter Class

, 28 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
C# Helper class to convert image to byte array and byte array to image

Introduction

Recently I was looking for a class which could convert a System.Drawing.Image to byte[] array and vice versa. After a lot of searching on Google, I realised that it would be faster for me to write this class and also share it with the community.

The class which I wrote is called ImageConverter.cs. The class has two methods.

First method: Convert Image to byte[] array:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}

This method uses the System.Drawing.Image.Save method to save the image to a memorystream. The memorystream can then be used to return a byte array using the ToArray() method in the MemoryStream class.

Second method: Convert byte[] array to Image:

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

This method uses the Image.FromStream method in the Image class to create a method from a memorystream which has been created using a byte array. The image thus created is returned in this method.

The way I happen to use this method was to transport an image to a web service, by converting it to a byte array and vice-versa.

Hope this class is useful to the community as well. The code of ImageConverter.cs can be downloaded from the link at the top of this article.

Rajan Tawate

Founder

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
Question[My vote of 1] not great Pin
BillW334-Dec-13 8:37
professionalBillW334-Dec-13 8:37 
AnswerRe: [My vote of 1] not great Pin
adriancs3-Oct-14 21:04
professionaladriancs3-Oct-14 21:04 
GeneralRe: [My vote of 1] not great Pin
BillW337-Oct-14 6:51
professionalBillW337-Oct-14 6:51 
QuestionImage Name and its Extenssion Pin
Nilesh Moradiya30-Oct-13 0:52
groupNilesh Moradiya30-Oct-13 0:52 
It's fine but how to get image name and its extension ?
GeneralGracias Pin
Sergio99923-Oct-13 12:18
memberSergio99923-Oct-13 12:18 
Questionthanks Pin
mqpabalan21-Oct-13 18:32
membermqpabalan21-Oct-13 18:32 
QuestionImage Retrieve from access DB using VB 2010 Pin
Member 999520324-May-13 2:29
memberMember 999520324-May-13 2:29 
GeneralMy vote of 5 Pin
BeamingJo7-May-13 23:27
memberBeamingJo7-May-13 23:27 
Questionvote 5 Pin
Mahdi_kishislan13-Mar-13 4:29
memberMahdi_kishislan13-Mar-13 4:29 
GeneralMy vote of 5 Pin
Member 950756223-Jan-13 3:47
memberMember 950756223-Jan-13 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 28 May 2009
Article Copyright 2006 by rajantawate1(http//www.tawateventures.com
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid