Can you try this function to convert to byte array
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
and pass the byte array for generating the image
public static Image CreateImage(byte[] imageData)
{
Image image;
using (MemoryStream inStream = new MemoryStream())
{
inStream.Write(imageData, 0, imageData.Length);
eladc
Member
32 Points
89 Posts
binary string to image?
May 07, 2012 12:13 PM|LINK
foreach (XmlNode customer in customerlist.SelectNodes("Customer")) { first_name = customer.SelectSingleNode("CustomerFirstName").InnerXml; last_name = customer.SelectSingleNode("CustomerLastName").InnerXml; total_orders = customer.SelectSingleNode("CustomerOrders").InnerXml; user_image = Convert.FromBase64String(customer.SelectSingleNode("CustomerPhoto").InnerXml); output.Text += "\n <tr>"; output.Text += "\n <td><img src=\"photo.png\"></td>"; output.Text += "\n <td>" + first_name + "</td>"; output.Text += "\n <td>" + last_name + "</td>"; output.Text += "\n <td>" + total_orders + "</td>"; }how do i use the variable user_image (of type byte[]) to display the user image ??
cninjas
Contributor
4868 Points
851 Posts
Re: binary string to image?
May 07, 2012 12:26 PM|LINK
hi
Can you try this function to convert to byte array
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
and pass the byte array for generating the image
public static Image CreateImage(byte[] imageData)
{
Image image;
using (MemoryStream inStream = new MemoryStream())
{
inStream.Write(imageData, 0, imageData.Length);
image = Bitmap.FromStream(inStream);
}
return image;
}
Hope it helps,thanks.
Niranjan
eladc
Member
32 Points
89 Posts
Re: binary string to image?
May 08, 2012 08:03 AM|LINK
the public static image gives me this error
Error 1 'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'
edit:
i got it to work .. my code had both 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'
.. any way now that it's working..
im getting back an image type how do i use it as an html image src?
cninjas
Contributor
4868 Points
851 Posts
Re: binary string to image?
May 08, 2012 09:53 AM|LINK
hi
try the below code for generating the image from byte array and assign the generated image to the control.
private void byteArrayToImage(byte[] byteArrayIn) { System.Drawing.Image newImage; string strFileName = GetTempFolderName() + "yourfilename.gif"; if (byteArrayIn != null) { using (MemoryStream stream = new MemoryStream(byteArrayIn)) { newImage = System.Drawing.Image.FromStream(stream); newImage.Save(strFileName); img.Attributes.Add("src", strFileName); } lblMessage.Text = "The image conversion was successful."; } else { Response.Write("No image data found!"); } }hope it helps.thanks,
Niranjan
eladc
Member
32 Points
89 Posts
Re: binary string to image?
May 08, 2012 10:45 AM|LINK
what's that ?
GetTempFolderName()
cninjas
Contributor
4868 Points
851 Posts
Re: binary string to image?
May 08, 2012 11:35 AM|LINK
hi
that is the function tat returns the path of the tempfoldername. Just return a string showing the path of the folder where you want to save the image.
Niranjan