Convert Image to ByteArray And ByteArray to Image in Asp.Net

Convert Image to ByteArray And ByteArray to Image in Asp.Net


Convert Image to ByteArray
Following code Convert PictureBox image (You can take manually image from any folder) to ByteArray
MemoryStream ms = new MemoryStream();
this.pictureBox2.Image.Save(ms, ImageFormat.Jpeg);
// now save the stream as a byte array
Byte[] imageBytes = new Byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin); // must rewind from the write
ms.Read(imageBytes, 0, (int)ms.Length);
ms.Close();
Now imageBytes Contains Image as a ByteArray.
Convert ByteArray to Image
Now Convert Above Bytearray in to Image.
System.IO.FileStream ms = new System.IO.FileStream(path_name + id_badge + ".jpg", System.IO.FileMode.Create, System.IO.FileAccess.Write);
//Where you Want to Save Image
//You Can Also Save In Control
ms.Write(imageBytes, 0, imageBytes.Length);
ms.Close();


Prev Post                                                      All Post                                                           Next Post


Posted in |

3 comments:

  1. Anonymous Says:

    Very good post..

  2. Anonymous Says:

    0

  3. Adnan_(AKESP) Says:

    Hi, I am getting folllowing Error

    Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Post a Comment