我已经在我的主机上上传了我的网络表单应用程序。在我的应用程序的一部分中,我尝试将图像和 int 的缩略图保存在 sql server 数据库中。我用来保存的字段的数据类型是 varbinary(max)。当我在我的电脑上测试应用程序时。它工作得很好,但是上传后我在尝试将图像保存在数据库中时遇到了这个错误。
我写的代码是这样的:
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[]));
}