Tina Wilson
02-25-2008, 07:33 AM
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 (http://system.io.fi/).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(imageFo rmat) );
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();
The error I get looks like this:
Exception Details: System.IO (http://system.io.fi/).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(imageFo rmat) );
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();