473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Image object not closing?

I've got users uploading a file, which I then want resize, make a thumbnail
out of, then delete. The image and thumbnail are working fine, but when I
try to delete the image, I get an error: "The process cannot access the file
"c:\inetpub\wwwroot\pictures\1\21_orig.jpg" because it is being used by
another process. "

I'm using the Image.Dispose() method before trying to delete it; how do I
free up this image?

Thanks in advance,

Duncan

---/ snip /---
private void SaveImage_Click(object sender, System.EventArgs e)
{
// Create data access layer
dal Dal = new dal();

// Create global params
config Config = new config();

string errorMessage = "";
string fileName = Image.PostedFile.FileName;
int extensionPos = 0;
string extension = "";

// Error checking
if (ImageTitle.Text == "")
{
errorMessage += "<li>You have not entered a title</li>";
}
if (fileName == "" && _CurrentImage_ID == 0)
{
errorMessage += "<li>You have not entered the file to upload</li>";
}
// We have a file - make sure it's of the right format!
if (fileName != "")
{
extensionPos = fileName.LastIndexOf(".") + 1;
if (extensionPos > 0)
{
extension = fileName.Substring(extensionPos, fileName.Length -
extensionPos);
}
if (extension != "gif" && extension != "jpg" && extension != "jpeg" &&
extension != "png")
{
errorMessage += "<li>Invalid file uploaded - Your image must be either GIF,
JPG, JPEG or PNG</li>";
}
}

// Do save or error
if (errorMessage == "")
{
if (fileName == "")
{
// It's an edit without a new image
// Get the existing image extension
extension = CurrentImage.Text;
// Write it!
Dal.PhotoAddEdit(_CurrentImage_ID, _CurrentPhotoAlbum_ID, ImageTitle.Text,
extension);
}
else
{
// It's an add, or an edit with a new image
int iNew_ID = Dal.PhotoAddEdit(_CurrentImage_ID, _CurrentPhotoAlbum_ID,
ImageTitle.Text, extension);
string newFileName = Config.ImageUploadDirectory +
_CurrentPhotoAlbum_ID.ToString() + "\\" + iNew_ID + "_orig." + extension;
string finalName = Config.ImageUploadDirectory +
_CurrentPhotoAlbum_ID.ToString() + "\\regular\\" + iNew_ID + "_orig." +
extension;
// Write the file to disk
Image.PostedFile.SaveAs(newFileName);
// Get the image and size
System.Drawing.Image image = System.Drawing.Image.FromFile(newFileName);
double height = image.Height;
double width = image.Width;
// Get the format
System.Drawing.Imaging.ImageFormat currentFormat;
if (extension == "gif")
{
currentFormat = System.Drawing.Imaging.ImageFormat.Gif;
}
else if (extension == "png")
{
currentFormat = System.Drawing.Imaging.ImageFormat.Png;
}
else
{
currentFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
}
// Get the new size
double newHeight = 0;
double newWidth = 0;
double scale = 0;
// Work out which way we're scaling this for the thumbnail
if (height > width)
{
scale = 100 / height;
newHeight = 100;
newWidth = width * scale;
}
else
{
scale = 100 / width;
newHeight = height * scale;
newWidth = 100;
}
// Create the thumbnail
System.Drawing.Image thumbnailImage =
image.GetThumbnailImage(System.Convert.ToInt32(new Width),
System.Convert.ToInt32(newHeight), new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback),
IntPtr.Zero);
// Write it!
thumbnailImage.Save(Config.ImageUploadDirectory +
_CurrentPhotoAlbum_ID.ToString() + "\\thumb\\" + iNew_ID + "." + extension,
currentFormat);
thumbnailImage.Dispose();
// If the main image > 800 x 600, we need to resize this, too
if (width > 800 || height > 600)
{
if (height > width)
{
scale = 600 / height;
newHeight = 600;
newWidth = width * scale;
}
else
{
scale = 800 / width;
newHeight = height * scale;
newWidth = 800;
}
System.Drawing.Image resizedImage =
image.GetThumbnailImage(System.Convert.ToInt32(new Width),
System.Convert.ToInt32(newHeight), new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback),
IntPtr.Zero);
// Write the fucker!
resizedImage.Save(finalName, currentFormat);
resizedImage.Dispose();
}
else
{
File.Copy(newFileName, finalName);
} // 800 x 600
// Get rid of the image
Image.Dispose();
// Get rid of the "original"
File.Delete(newFileName);
}
// All saved - redirect!
Response.Redirect("managephotos.aspx?photoAlbum_ID =" +
_CurrentPhotoAlbum_ID);
}
else
{
ErrorMessage.Text = "The following errors have occurred: <ul>" +
errorMessage + "</ul>Click <a
href=\"javascript:window.history.go(-1);\">here</a> to go back and correct
them.";
ShowPanel(ErrorPanel);
}
}

public bool ThumbnailCallback()
{
return false;
}
Nov 16 '05 #1
0 1629

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tlviewer | last post by:
hello, The script below is a prototype for loading playing card images from the bitmap resource in cards.dll (WinXP) I like the idea of not keeping copies of the images as files. I'm able...
11
by: Stephen Poley | last post by:
I'd like to float an image to the bottom of a DIV containing several paragraphs, so that it is positioned to the right of the closing paragraphs (and preferably below the closing paragraphs if the...
6
by: Saya | last post by:
Hello, This is a repost 'cause I haven't solve the problem: I can't use the System.Drawing class 'Image.FromStream' in the CompactFramework environment. What I've done with respect to Brendan's...
2
by: Dave | last post by:
I'm having trouble understanding dispose. I set up a class that, among other things, displays the time in a status bar panel. It does this by starting a thread. I create an instance of this...
39
by: jcrouse | last post by:
I am using the following code to get a background image for my form Private Sub mnuBgroundImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBgroundImage.Clic If...
6
by: Patrick Dugan | last post by:
Hello, I'm trying to load different images (icons) into a PictureBox1.Image. The first image loads just fine, but the second image always returns the error "Invalid property used." It doesn't...
5
by: Camet | last post by:
I have been trying to create a dynamic id for a number of images in a table so that I can identify which image was clicked on later. ie I need to set a variable to the id of each image, that is...
5
by: mikez | last post by:
Hi, We recently built a very basic file management system for a client in Access 2003 (to use with incoming tif scans). In it we used Microsoft's Document Imaging activex viewer (from Office...
6
by: Mark Denardo | last post by:
I created a Web Image "<asp:Image ID="Image1" ..." that my code behind set to a certain image file say image1.jpg =Image1.ImageUrl = "<rel_path>/image1.jpg"; which set the image ok, but then I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.