在数据库中保存图像时GDI中发生一般错误

时间:2013-10-25 12:53:57

标签: c# asp.net image gdi

我已在我的主机上上传了我的网络表单应用程序。在我的应用程序的一部分,我尝试在sql server数据库中保存图像和int的缩略图。我用来保存的字段的数据类型是varbinary(max)。当我在我的电脑上测试应用程序时。它运行良好,但上传后我尝试将图像保存在数据库中时出现此错误。 enter image description here

我写的代码是这样的:

        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[]));
    }

0 个答案:

没有答案