I am uploading an image to my database using the file upload control. I also have a method that is creating a thumbnail and saving THAT to the database from the original image. The error I get looks like this: Exception Details: System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv\Sunset.jpg'. Even though the original image is here: C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures My question is where is it getting that first path from and why would it matter if the file was found or not since I'm not saving anything to disk....it'sall being saved to the database. HELP! Here is my method that is creating the thumbnail: public byte[] GetThumbnailImage(string fileName) { int thumbnailHeight; int thumbnailWidth; int maxThumbSize = 150; double hx; double wx; System.Drawing.Image myimage; myimage = System.Drawing.Image.FromFile(fileName); SessionState.ImageHeight = myimage.Height; SessionState.ImageWidth = myimage.Width; hx = myimage.Height; wx = myimage.Width; // calc the thumbnail Width and Height: -scale to max with if (myimage.Width > myimage.Height) // landscape: width is fixed,scale height: { thumbnailWidth = maxThumbSize; thumbnailHeight = Convert.ToInt32((hx / wx) * thumbnailWidth); } else // portrait: height is fixed at max, scale width: { thumbnailHeight = maxThumbSize; thumbnailWidth = Convert.ToInt32((wx / hx) * thumbnailHeight); } System.Drawing.Image thumbnailImage = myimage.GetThumbnailImage(thumbnailWidth, thumbnailHeight, null, IntPtr.Zero); MemoryStream ms = new MemoryStream(); string imageFormat = Path.GetExtension(ImageUpload.PostedFile.FileName).Replace(".", "").ToUpper(); thumbnailImage.Save(ms,this.GetImageFormat(imageFormat) ); return ms.ToArray(); } Here is the code that is part of the save button that saves the original image to the DB: Photo photo = new Photo(); photo.CategoryId = Convert.ToInt32(this.CategoryList.SelectedValue); photo.PhotoName = this.PhotoName.Text; photo.UserId = this.UserId; byte[] image; string fileName = ImageUpload.PostedFile.FileName; //// Open File and Read Into Byte Array using (FileStream fs = new FileStream(fileName, FileMode.Open)) { BinaryReader reader = new BinaryReader(fs); image = reader.ReadBytes((int)fs.Length); fs.Close(); } photo.ImageFormat = Path.GetExtension(ImageUpload.PostedFile.FileName).Replace(".", "").ToUpper(); photo.ContentType = ImageUpload.PostedFile.ContentType; photo.PhotoMember = image; photo.Thumbnail = this.GetThumbnailImage(fileName); photo.Caption = this.Caption.Text; photo.DateCreated = DateTime.Now; photo.ContentLength = ImageUpload.PostedFile.ContentLength; photo.ImageHeight = this.ImageHeight; photo.ImageWidth = this.ImageWidth; photo.Save();
You have a Memory Stream and a File Stream but no paths or connections. btw, do you really need to dump files into the Database? All the best, Mark
What do you mean paths or connections? The file stream has the path of the original file it is uploading and I pass that same path into the method that is creating the thumbnail. string fileName = ImageUpload.PostedFile.FileName; //// Open File and Read Into Byte Array using (FileStream fs = new FileStream(fileName, FileMode.Open)) { BinaryReader reader = new BinaryReader(fs); image = reader.ReadBytes((int)fs.Length); fs.Close(); } ...and if you mean connection to the database that is handled somewhere else.This works on my local machine BTW, just not in my deployed version on discountasp.net. The database part was not my choice! :0( It was required by management.
OK, was just a bit mixed up looking at the code without the missing parts. Try the link below using my ASP.NET Coop, will pull up a lot of examples using this method: http://www.google.com/custom?hl=en&client=google-coop&cof=AH:left%3BCX:Google%2520Wise%3BL:http://www.google.com/coop/intl/en/images/custom_search_sm.gif%3BLH:65%3BLP:1%3BGFNT:%23666666%3BDIV:%23cccccc%3B&cx=009244845836111659857:lbvxsjj8mho&adkw=AELymgW9UJ5qbGFJ9YERc_-wW2760oHaQMmXM6rRjyoC7g-X4UGjmHCkM9gTyjoGLk2zgMPZn8f6lSAdGy7LQRDlKjZj9rWZqelPPxrEFWdoJW8ZE_Fc6tg&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=asp.net+upload+image+to+database&spell=1 That's a big URL but it's safe.