I've uploaded my web form application on my host. in a part of my application, i try to save the image and int's thumbnail in sql server database. the data type of fields im using to save is varbinary(max). when I test the application on my computer. it works good but after uploading I got this error when trying to save the image in database.
The code I've written is like this:
NowbakhtEntities entities = new NowbakhtEntities();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsert_Click(object sender, EventArgs e)
{
if (ImageUpload.HasFile)
{
Byte[] contents = ImageUpload.FileBytes;
MemoryStream imgStream = new MemoryStream(contents);
Byte[] thumb = null;
System.Drawing.Image img1 = new Bitmap(imgStream);
if (img1.Height < img1.Width)
{
int x = Convert.ToInt32((150*img1.Height)/img1.Width);
System.Drawing.Image bmp2 = img1.GetThumbnailImage(150, x, null, IntPtr.Zero);
thumb = ImageToByte(bmp2);
bmp2.Dispose();
}
else
{
int x = Convert.ToInt32((150*img1.Width)/img1.Height);
System.Drawing.Image bmp2 = img1.GetThumbnailImage(x, 150, null, IntPtr.Zero);
thumb = ImageToByte(bmp2);
bmp2.Dispose();
}
Project project = new Project();
project.Title = txtTitle.Text;
project.Description = txtDesc.Content;
project.Image = contents;
project.Thumb = thumb;
entities.Projects.Add(project);
entities.SaveChanges();
img1.Dispose();
imgStream.Close();
Response.Redirect("ProjectList.aspx");
}
}
public byte[] ImageToByte(System.Drawing.Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}