0

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. enter image description here

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[]));
    }
AnonJr
  • 2,759
  • 1
  • 26
  • 39
Mr.B
  • 63
  • 2
  • 9
  • 1
    That particular error is a horrible one. It can mean anything from invalid properties/constructor arguments to invalid save permissions. You really have to debug and step through it line by line where its failing. Also, try building your application in Release locally and see if it fails. Then try running Visual Studio with an account that has less privileges and see if it still runs fine.. etc. Hence "generic". – Simon Whitehead Oct 25 '13 at 13:04
  • I've tried to recompile, republish, rebuilt and everything you can guess. I also checked the host folder permissions. Everything looks ok but didnt work :( – Mr.B Oct 25 '13 at 13:08
  • What stream are you saving it to? MemoryStream? Can you show us the ImageToByte method? – Simon Whitehead Oct 25 '13 at 13:10
  • if you scroll down the code, you can see ImageToByte method – Mr.B Oct 25 '13 at 13:18
  • Oh wow.. I'm really not in the zone tonight :/ Thankfully its Friday. – Simon Whitehead Oct 25 '13 at 13:18
  • Sounds like it's related to this: http://stackoverflow.com/q/9073619/56778. To avoid the problem, you can probably modify your `ImageToByte` so that rather than using `ImageConverter` it writes to a `MemoryStream` as shown in this answer: http://stackoverflow.com/a/7350818/56778 – Jim Mischel Oct 29 '13 at 18:54

0 Answers0