Convert image to binary and back to image
-
Hi, Been checking around the net for the above subject and found a lot of solutions but seems not able to work for me. This is my situation, I am using a textbox with type='file' to get the image from local. Then when user clicks a button, it suppose to convert the picture user chose into binary data and save it into the MSSQL database which in the column declared as binary(5000). When the user load the page again, I want the system to get the binary data from the database, covert it back to the original format, and place it in the tag. Here is the code I'm using but I suspect something is just not right.
byte[] imagecontent = null;
if (fudLogo.Value != "")
{
//If got logo need upload, change it to binary format
string strfile = System.IO.Path.GetFileName(fudLogo.PostedFile.FileName);
string strexten = System.IO.Path.GetExtension(fudLogo.PostedFile.FileName);
if (strexten.Equals(".jpg") || strexten.Equals(".JPG") || strexten.Equals(".gif") || strexten.Equals(".GIF") || strfile.Equals(""))
{
System.IO.Stream stream;
Int64 imagesize;
int imgstatus;
imagesize = fudLogo.PostedFile.ContentLength;
stream = fudLogo.PostedFile.InputStream;
imagecontent = new byte[stream.Length];
}
}
else
{
imagecontent = this.strCpyLogo;
}//Update to database if (this.staff.UpdateCompany(this.txtCompanyName.Text, imagecontent, this.txtAddress1.Text, this.txtAddress2.Text, this.txtAddress3.Text, this.txtPostCode.Text, this.txtState.Text, this.txtCountry.Text, Application\["compid"\].ToString()) == true) { Response.Redirect("E09-WorkingDayM.aspx?compid=" + Application\["compid"\].ToString() + "&empid=" + this.strEmpId + "&resetting=1"); } else { this.lblErrMsg.Text = "Could not update company info at this moment."; }
I tried to view the raw binary data just to check whether it is right by using below code
string aa = "";
for (int i = 0; i < imagecontent.Length - 1; i++)
{
aa += imagecontent[i].ToString();
} -
Hi, Been checking around the net for the above subject and found a lot of solutions but seems not able to work for me. This is my situation, I am using a textbox with type='file' to get the image from local. Then when user clicks a button, it suppose to convert the picture user chose into binary data and save it into the MSSQL database which in the column declared as binary(5000). When the user load the page again, I want the system to get the binary data from the database, covert it back to the original format, and place it in the tag. Here is the code I'm using but I suspect something is just not right.
byte[] imagecontent = null;
if (fudLogo.Value != "")
{
//If got logo need upload, change it to binary format
string strfile = System.IO.Path.GetFileName(fudLogo.PostedFile.FileName);
string strexten = System.IO.Path.GetExtension(fudLogo.PostedFile.FileName);
if (strexten.Equals(".jpg") || strexten.Equals(".JPG") || strexten.Equals(".gif") || strexten.Equals(".GIF") || strfile.Equals(""))
{
System.IO.Stream stream;
Int64 imagesize;
int imgstatus;
imagesize = fudLogo.PostedFile.ContentLength;
stream = fudLogo.PostedFile.InputStream;
imagecontent = new byte[stream.Length];
}
}
else
{
imagecontent = this.strCpyLogo;
}//Update to database if (this.staff.UpdateCompany(this.txtCompanyName.Text, imagecontent, this.txtAddress1.Text, this.txtAddress2.Text, this.txtAddress3.Text, this.txtPostCode.Text, this.txtState.Text, this.txtCountry.Text, Application\["compid"\].ToString()) == true) { Response.Redirect("E09-WorkingDayM.aspx?compid=" + Application\["compid"\].ToString() + "&empid=" + this.strEmpId + "&resetting=1"); } else { this.lblErrMsg.Text = "Could not update company info at this moment."; }
I tried to view the raw binary data just to check whether it is right by using below code
string aa = "";
for (int i = 0; i < imagecontent.Length - 1; i++)
{
aa += imagecontent[i].ToString();
}J Liang wrote:
imagecontent = new byte[stream.Length];
This is the problem. You are initializing a new byte array and not copying stream contents to this array.
if (strexten.Equals(".jpg") || strexten.Equals(".JPG") || strexten.Equals(".gif") || strexten.Equals(".GIF") || strfile.Equals(""))
{
System.IO.Stream stream;
Int64 imagesize;
int imgstatus;
imagesize = fudLogo.PostedFile.ContentLength;
stream = fudLogo.PostedFile.InputStream;
imagecontent = new byte[stream.Length];
stream.Read(imagecontent,0,imagecontent.Length);
}All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
J Liang wrote:
imagecontent = new byte[stream.Length];
This is the problem. You are initializing a new byte array and not copying stream contents to this array.
if (strexten.Equals(".jpg") || strexten.Equals(".JPG") || strexten.Equals(".gif") || strexten.Equals(".GIF") || strfile.Equals(""))
{
System.IO.Stream stream;
Int64 imagesize;
int imgstatus;
imagesize = fudLogo.PostedFile.ContentLength;
stream = fudLogo.PostedFile.InputStream;
imagecontent = new byte[stream.Length];
stream.Read(imagecontent,0,imagecontent.Length);
}All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thanks, it works now. I got this 25521625522401674707370011107207200255225310069120105102007373420800070151201000098000161209000108000 491202700011800050120.... Is this what I suppose to get? The numbers do turn different as I upload different pictures. Now I know there is a lot of solutions out there to turn the above binary back to an image file but I just could not find one working, is there any code which any of you are using that is working? Can shade some light for me? Thanks.
-
Thanks, it works now. I got this 25521625522401674707370011107207200255225310069120105102007373420800070151201000098000161209000108000 491202700011800050120.... Is this what I suppose to get? The numbers do turn different as I upload different pictures. Now I know there is a lot of solutions out there to turn the above binary back to an image file but I just could not find one working, is there any code which any of you are using that is working? Can shade some light for me? Thanks.
J Liang wrote:
Is this what I suppose to get? The numbers do turn different as I upload different pictures.
It depends on the file you are uploading. This will change for image to image.
J Liang wrote:
out there to turn the above binary back to an image file
using (MemoryStream stream = new MemoryStream(b))
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
{
// Use the image variable here
}
}Hope this helps
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Thanks, it works now. I got this 25521625522401674707370011107207200255225310069120105102007373420800070151201000098000161209000108000 491202700011800050120.... Is this what I suppose to get? The numbers do turn different as I upload different pictures. Now I know there is a lot of solutions out there to turn the above binary back to an image file but I just could not find one working, is there any code which any of you are using that is working? Can shade some light for me? Thanks.
To set that image to a image control, create a seperate page say "DisplayImage.aspx". As I said in the last post, take
Image
instance and callimage.Save(Response.OutputStream,ImageFormat.Jpeg)
which save the image to output stream. The image format varies depending on your image extension. Also don't forget to set the response content type likeResponse.ContentType = "image/jpeg"
(Only for jpg). In the image control, set image url as this page(DisplayImage.aspx), you are done.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
J Liang wrote:
Is this what I suppose to get? The numbers do turn different as I upload different pictures.
It depends on the file you are uploading. This will change for image to image.
J Liang wrote:
out there to turn the above binary back to an image file
using (MemoryStream stream = new MemoryStream(b))
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
{
// Use the image variable here
}
}Hope this helps
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
I'm not really understand this line
// Use the image variable here
Do you mean to assign System.Drawing.Image image into my
This is what I did from your code given
using (MemoryStream stream = new MemoryStream(strCpyLogo))
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
{
// Use the image variable here} }
Where strCpyLogo is in type byte which I retrieve from my database. In the database, strCpyLogo shows which i assumed it had already saved into the database. I'd tried this.imgLogo but could not find any command that seems logic to me to assign System.Drawing.Image image to it.
-
To set that image to a image control, create a seperate page say "DisplayImage.aspx". As I said in the last post, take
Image
instance and callimage.Save(Response.OutputStream,ImageFormat.Jpeg)
which save the image to output stream. The image format varies depending on your image extension. Also don't forget to set the response content type likeResponse.ContentType = "image/jpeg"
(Only for jpg). In the image control, set image url as this page(DisplayImage.aspx), you are done.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
I got a error message when I run the code Exception Details: System.ArgumentException: Parameter is not valid. on this line using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream)) I am doing this in my code
byte[] strCpyLogo;
Response.ContentType = "image/jpeg";
using (MemoryStream stream = new MemoryStream(strCpyLogo)) { using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream)) { // Use the image variable here } }
Where strCpyLogo was a binary data from the database
-
I got a error message when I run the code Exception Details: System.ArgumentException: Parameter is not valid. on this line using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream)) I am doing this in my code
byte[] strCpyLogo;
Response.ContentType = "image/jpeg";
using (MemoryStream stream = new MemoryStream(strCpyLogo)) { using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream)) { // Use the image variable here } }
Where strCpyLogo was a binary data from the database
J Liang wrote:
System.ArgumentException: Parameter is not valid.
Image class throws really weird errors. In the code shown here, you have not filled the byte array.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
J Liang wrote:
System.ArgumentException: Parameter is not valid.
Image class throws really weird errors. In the code shown here, you have not filled the byte array.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
After some checking, I tried to do a select statement in Mssql and found out that there are values in the image column of my table (a bunch of alpha numeric) I'd tried checking the strCpyLogo.Length and it only show me 26. Now I really lost, not really sure where should I go now. The piece of code you show me earlier
using (MemoryStream stream = new MemoryStream(strCpyLogo))
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
{} }
Is this suppose to help me create a physical jpg file and allow my tag to point to that jpg file? I saw a few example which is uses similar approach as you shown me here.
-
After some checking, I tried to do a select statement in Mssql and found out that there are values in the image column of my table (a bunch of alpha numeric) I'd tried checking the strCpyLogo.Length and it only show me 26. Now I really lost, not really sure where should I go now. The piece of code you show me earlier
using (MemoryStream stream = new MemoryStream(strCpyLogo))
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
{} }
Is this suppose to help me create a physical jpg file and allow my tag to point to that jpg file? I saw a few example which is uses similar approach as you shown me here.
I think i realized there is a place where could cause all these. I uses a stored procedure to get the binary data out but I'd declare in my coding as byte[]. So it gives me a problem when I do
SqlParameter objParam2;
objParam2 = myCommand.Parameters.Add("@cpyLogo", SqlDbType.Binary, 5000);
objParam2.Direction = ParameterDirection.Output;
myCommand.ExecuteNonQuery();
if (objParam2.Value == DBNull.Value)
{
cpyLogo = null;
}
else
{
cpyLogo = Convert.ToByte(objParam2.Value); <--Note it is not converting to byte[]
}So I found in the internet where they uses this function to convert to byte array
public static byte[] ConvertStringToByteArray(string stringToConvert)
{
return (new UnicodeEncoding()).GetBytes(stringToConvert);
}And I changed my code to
if (objParam2.Value == DBNull.Value)
{
cpyLogo = null;
}
else
{
cpyLogo = ConvertStringToByteArray(objParam2.Value.ToString());
}Later I realized I'd read some where that I should not threat binary data as string (objParam2.Value.ToString()), could this be the problem? Or there is another way to change my objParam2.Value to byte array? I think now what I need to do is to find out how to convert binary to byte array first.
modified on Thursday, June 12, 2008 5:21 AM