Reputation: 61
Problem: First, My "Create" controller action method creates two files two different ways. However, my program is then unable to delete the file which was created using file.SaveAs(path); I can however successfully delete the other file which was created using imgPhoto.Save(smallImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
Here is the HttpPost overload of my Create controller action method including a ScaleByPercent method call: (full error message pasted at bottom)
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Create(HttpPostedFileBase file, Models.Gallery gallerycm)
{
ViewBag.Message = "Testing Gallery File Create";
if (file != null && file.ContentLength > 0)
try
{
string path = Path.Combine(Server.MapPath("~/Images/demo/gallery"),
Path.GetFileName(file.FileName));
//System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal);
//System.Drawing.Image MainImgPhotoVert = System.Drawing.Image.FromFile(path);
/*
System.Drawing.Image MainImgPhotoVert = System.Drawing.Image.FromStream(System.IO.Stream file);
Bitmap MainImgPhoto = (System.Drawing.Bitmap)ScaleByPercent(MainImgPhotoVert, 100);
MainImgPhoto.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
MainImgPhoto.Dispose();
*/
file.SaveAs(path);
file.InputStream.Flush(); //useless
file.InputStream.Close(); //less than useless
file.InputStream.Dispose(); //complete waste of keystrokes
//System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal);
// Validating whether the following commented code releases a recently created
// file from IIS for file Delete. Problem occuring in the Visual Studio test environment.
//file.InputStream.Dispose();
//GC.Collect();
//GC.WaitForPendingFinalizers();
// Create the Thumbnail image
string smallImageFilePath = Path.Combine(Server.MapPath("~/Images/demo/gallery/") + "ThumbSize" + (file.FileName));
//allocate an Image object from the uploaded full sized .jpg
System.Drawing.Image imgPhotoVert = System.Drawing.Image.FromFile(path);
Bitmap imgPhoto = (System.Drawing.Bitmap)ScaleByPercent(imgPhotoVert, 50);
imgPhoto.Save(smallImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
var gallery = new Gallery();
//gallery.PhotoNumberID = 9;
gallery.Filename = file.FileName;
if (gallerycm.PhotoDescription == null)
gallerycm.PhotoDescription = " ";
gallery.PhotoDescription = gallerycm.PhotoDescription;
var galleryContext = new EFDbGalleryContext();
galleryContext.Gallery.Add(gallery);
galleryContext.SaveChanges();
}
catch (Exception ex)
{
TempData["SomeData"] = file.FileName + " Upload exception. The Details follow: " + ex.ToString();
return RedirectToAction("Index");
}
else
{
ViewBag.Message = "You have not specified a file.";
}
TempData["SomeData"] = "Photo was successfully Added";
return RedirectToAction("Index");
}
static System.Drawing.Image ScaleByPercent(System.Drawing.Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent / 100);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
}
Here is an excerpt from my Delete controller action:
//Delete small and large files from the FileSystem
System.IO.File.Delete(smallImageFilePath);
//System.IO.File.GetAccessControl(largeImageFilePath);
try
{
System.IO.File.GetAccessControl(largeImageFilePath); // Does not help
System.IO.File.Delete(largeImageFilePath);
}
catch (System.IO.IOException e)
{
TempData["SomeData"] = " Delete exception. The Details follow: " + e.ToString();
return RedirectToAction("Index");
}
Note also: If I pause the program in VS Debug I am able to delete the Bitmap file in Windows Explorer but the other returns:
"File in Use" "The Action can't be completed because the File is Open in IIS Worker Process. Close the File and Try Again." If I close VS and go back in I can delete it, but that doesn't help me when I deploy everything on my Godaddy server.
Here is the total message: Delete exception. The Details follow: System.IO.IOException: The process cannot access the file 'C:\aspnet4_cs\Pettigoats\Pettigoats\Images\demo\gallery\WalkingOnPorch.jpg' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at Pettigoats.Controllers.CMAdminController.Delete(Int32 id) in c:\aspnet4_cs\Pettigoats\Pettigoats\Controllers\CMAdminController.cs:line 53
Upvotes: 1
Views: 3389
Reputation: 99
GDI+ puts a lock on the file, for more info GDI+ Graphics
a Dispose() after the save might do the trick
Upvotes: 1