Reputation:
I'm facing an issue when trying to select an image field from the SQL server.
Here's the select code:
string cmd = String.Format("SELECT TOP 1 * FROM {0}", DefTableNames.Employees.ToString());
DataTable data = SQLDataQueryToTable(FullServerPath(), cmd);
CurrentEditingIndex = int.Parse(data.Rows[0][0].ToString());
empNameDropDown.SelectedItem = empNameDropDown.Items[0];
empNameTxt.Texts = data.Rows[0][1].ToString();
empIdTxt.Texts = data.Rows[0][2].ToString();
empPhoneTxt.Texts = data.Rows[0][3].ToString();
empEducationTxt.Texts = data.Rows[0][4].ToString();
empJobNameTxt.Texts = data.Rows[0][5].ToString();
TextTools.Currencize(data.Rows[0][6].ToString(), empTotalAllTxt);
empAboutTxt.Texts = data.Rows[0][7].ToString();
empBirthDatePicker.Value = DateTime.Parse(data.Rows[0][8].ToString(), CultureInfo.InvariantCulture);
empJobDatePicker.Value = DateTime.Parse(data.Rows[0][9].ToString(), CultureInfo.InvariantCulture);
Console.WriteLine(data.Rows[0][10]);
image_employeeimage.Image = byteArrayToImage((byte[])data.Rows[0][10], null);
if (image_employeeimage.Image != null)
{
buttonDeleteImage.Enabled = true;
buttonDeleteImage.BackColor = Color.Brown;
}
else
{
buttonDeleteImage.Enabled = false;
buttonDeleteImage.BackColor = AColors.DisabledColor;
}
empUserDropDown.SelectedItem = data.Rows[0][11].ToString();
The code fills everything in properly, however, it keeps stopping at the method for conversion of the data to an image.
Here's the conversion code:
public static Image byteArrayToImage(byte[] byteArrayIn, Image defaultImage = null)
{
if (byteArrayIn == null)
{
return defaultImage;
}
Image returnImage;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}
The code stops exactly at here:
Here's the code that converts to byte[]
:
public static byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
When I try to do a Console.WriteLine
for the value of the image, I get the proper value. Which is System.Byte[]
.
The type of column in the database is set to VarBinary
.
What exactly am I doing wrong?
Upvotes: 0
Views: 72
Reputation:
I found my problem. The issue was pretty dumb:
public static byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
I was saving the image as a gif. Which when I changed to this:
public static byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, imageIn.RawFormat);
return ms.ToArray();
}
Fixed the entire issue.
And I also found a big problem in the save code. I was defining the size of the data as 255. This also led to the images not loading at times or loading as null. This was in the original code that I didn't share:
if(image_employeeimage.Image == null)
{
sqlParams.Add(new SqlParameter("@image", SqlDbType.VarBinary, 255)); sqlParams[9].Value = DBNull.Value;
}
else
{
sqlParams.Add(new SqlParameter("@image", SqlDbType.VarBinary, 255)); sqlParams[9].Value = imageToByteArray(image_employeeimage.Image);
}
I simply removed 255.
Everything seems fine now. Thanks a lot for the help everyone, and thanks for the tips on my code.
If there are any comments on my fix, please share them.
Upvotes: 2