Simply you can use the following code to convert Byte Array to Image:
Image.FromStream(new MemoryStream(byteArrayIn));
Which you can put it in a function like this:
public Image byteArrayToImage(byte[] byteArrayIn)
{
Image returnImage = null;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}
You can also convert your Byte Array to string and use it to bind a PictureBox like the following code. Actually, I've used it in WebApp projects and not sure if it works on yours:
string photo = "data:image/jpeg;base64," + Convert.ToBase64String(byteArray.Photo, 0, byteArray.Photo.Length);
Answer from Majid Shahabfar on Stack Overflowc# - How to I convert to image from Byte[] Array? - Stack Overflow
Converting a byte array into an image
javascript - How to convert a byte array into an image?
c# - How to convert a byte array to an image? - Stack Overflow
Simply you can use the following code to convert Byte Array to Image:
Image.FromStream(new MemoryStream(byteArrayIn));
Which you can put it in a function like this:
public Image byteArrayToImage(byte[] byteArrayIn)
{
Image returnImage = null;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}
You can also convert your Byte Array to string and use it to bind a PictureBox like the following code. Actually, I've used it in WebApp projects and not sure if it works on yours:
string photo = "data:image/jpeg;base64," + Convert.ToBase64String(byteArray.Photo, 0, byteArray.Photo.Length);
From PictureBox to DB (VB Code)
Dim fs As FileStream = File.Create("profile.jpg")
ProfilePictureBox.Image.Save(fs, Imaging.ImageFormat.Jpeg)
fs.Close()
fs = File.OpenRead("profile.jpg")
Dim ms As MemoryStream = New MemoryStream
fs.CopyTo(ms)
sqlCmd.Parameters.Add("@profilePic", SqlDbType.VarBinary).Value = ms.ToArray()
From Db To PictureBox (VB Code)
Dim readByte As Byte() = sqlReader("IMG_BYTE")
Dim ms As MemoryStream = New MemoryStream(readByte)
PictureBox1.Image= Image.FromStream(ms)
From Db To DatagridView (VB Code)
Dim img As Image
Dim readByte As Byte() = sqlReader("IMG_BYTE")
Dim ms As MemoryStream = New MemoryStream(readByte)
img = Image.FromStream(ms)
I realize this is an old thread, but I managed to do this through an AJAX call on a web service and thought I'd share...
I have an image in my page already:
<img id="ItemPreview" src="" />AJAX:
$.ajax({ type: 'POST', contentType: 'application/json; charset=utf-8', dataType: 'json', timeout: 10000, url: 'Common.asmx/GetItemPreview', data: '{"id":"' + document.getElementById("AwardDropDown").value + '"}', success: function (data) { if (data.d != null) { var results = jQuery.parseJSON(data.d); for (var key in results) { //the results is a base64 string. convert it to an image and assign as 'src' document.getElementById("ItemPreview").src = "data:image/png;base64," + results[key]; } } } });
My 'GetItemPreview' code queries a SQL server where I have an image stored as a base64 string and returns that field as the 'results':
string itemPreview = DB.ExecuteScalar(String.Format("SELECT [avatarImage] FROM [avatar_item_template] WHERE [id] = {0}", DB.Sanitize(id)));
results.Add("Success", itemPreview);
return json.Serialize(results);
The magic is in the AJAX call at this line:
document.getElementById("ItemPreview").src = "data:image/png;base64," + results[key];
Enjoy!
Converting a byte array to base64 when you have the binary byte array (not a JSON string array of the byte values) is ridiculously expensive, and more importantly; it is totally unnecessary work, as you do not have to do convert it at all in modern browsers! The static URL.createObjectURL method creates a DOMString, a short browser-specific url, from the byte array, and you can use the resulting short string in img.src or similar.
This is infinitely faster than solutions that require chaining TextEncoder and btoa when all you need is to display an image received in a byte array form.
var blob = new Blob( [ uint8ArrayBuffer ], { type: "image/jpeg" } );
var imageUrl = URL.createObjectURL( blob );
This is using HTML5 APIs, and so will not work on Node or other JS based servers, of course.
// Simulate a call to Dropbox or other service that can
// return an image as an ArrayBuffer.
var xhr = new XMLHttpRequest();
// Use PlaceKitten as a sample image to avoid complicating
// this example with cross-domain issues.
xhr.open( "GET", "https://placekitten.com/200/140", true );
// Ask for the result as an ArrayBuffer.
xhr.responseType = "arraybuffer";
xhr.onload = function( e ) {
// Obtain a blob: URL for the image data.
var arrayBufferView = new Uint8Array( this.response );
var blob = new Blob( [ arrayBufferView ], { type: "image/jpeg" } );
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL( blob );
var img = document.querySelector( "#photo" );
img.src = imageUrl;
};
xhr.send();
<h1>Demo of displaying an ArrayBuffer</h1>
<p><a href="http://jsfiddle.net/Jan_Miksovsky/yy7Zs/">Originally made by Jan Miksovsky</p>
<img id="photo"/>
You don't need Bitmap class. All you need is base64 encoded data as below
imbThumbnail.ImageUrl = "data:image/jpeg;base64,"+ Convert.ToBase64String(data);
Try this.
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
Source : C# Image to Byte Array and Byte Array to Image Converter Class
You are writing to your memory stream twice, also you are not disposing the stream after use. You are also asking the image decoder to apply embedded color correction.
Try this instead:
using (var ms = new MemoryStream(byteArrayIn))
{
return Image.FromStream(ms);
}
Maybe I'm missing something, but for me this one-liner works fine with a byte array that contains an image of a JPEG file.
Image x = (Bitmap)((new ImageConverter()).ConvertFrom(jpegByteArray));
EDIT:
See here for an updated version of this answer: How to convert image in byte array