Loading ...

ASP.NET: Bind System.Drawing.Image to ASP.NET Image Control | CodeAsp.Net

ASP.NET: Bind System.Drawing.Image to ASP.NET Image Control

 /5
0 (0votes)

If you have the image data in System.Drawing.Image and want to bind that to ASP.NET Image control following code will help you:

System.Drawing.Image img = System.Drawing.Image.FromStream(xx);  // Your source image
string base64ImageString = ConvertImageBytesToBase64(ImageToByteArray(img));
contactImage.ImageUrl = string.Format("data:image/jpg;base64,{0}", base64ImageString);

Note the ConvertImageBytesToBase64 method used above. That method is responsible for converting the image to bytes, here is the code for it:

public string ConvertImageBytesToBase64(byte[] imageBytes)
{
return Convert.ToBase64String(imageBytes);
}


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

After converting the image to bytes I have set that to ImageURL property.

Comments (no comments yet)

Top Posts