Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,
I am trying to convert image to byte format to save it into the database.
I am using sqlserver 2008.
I wrote some code. When I try to execute the code line by line, the cursor is checking only one line. From there cursor comes back. the line is below:

byte[] myimage = new byte[FUImage.PostedFile.ContentLength];


Can anybody please help me.
The compiler is checking only this line. From here it is going to the catch block.
Posted
Updated 1-Nov-10 3:53am
v2

I' guessing you're running into a null reference exception.
When you're about to execute that line, check the values of
FUImage, FUImage.PostedFile and FUImage.PostedFile.ContentLength

One of those is probably null.
Also, it would be easier to provide help if you provided the exception details.

Hope this helps,
Fredrik
 
Share this answer
 
look :
C#
private static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert,
ImageFormat formatOfImage)
{
byte[] Ret;

try
{

using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms,formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw;}

return Ret;
}


When you are ready to convert the byte array back
to an image, you can include the following code
in your method.

C#
System.Drawing.Image newImage;


using (MemoryStream ms = new MemoryStream(myByteArray,0,myByteArray.Length))
{

ms.Write(myByteArray,0,myByteArray.Length);

newImage = Image.FromStream(ms,true);

// work with image here.

// You'll need to keep the MemoryStream open for
// as long as you want to work with your new image.

}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900