Hi,
I'm having a problem with releasing a file lock after a save. My code takes an image file uploaded via a form, and saves it to disk. That's fine, but I'm trying to add a new function which allows the user to choose to automatically create a thumbnail. At this stage, it throws a "System.OutOfMe moryException: Out of memory." Even if the files are small, I still get this, and I find that if I open up Windows Explorer and try to delete the large image, it's locked by the aspnet_wp process, so I'm guessing it's this lock that's causing the exception (which I know doesn't always mean it's actually out of memory!).
I have tried everything I can think of to release the lock (see the gubbins below!) but it's still not working. Any ideas? -
public static Upload uploadFile(HtmlInputFile inputFile, string sSavePath, bool createThumb)
-
{
-
System.Web.HttpServerUtility Server=HttpContext.Current.Server;
-
-
// If file field isn't empty
-
if (inputFile.PostedFile.ContentLength > 0)
-
{
-
-
// Check file size (mustn't be 0)
-
HttpPostedFile myFile = inputFile.PostedFile;
-
int iFileLen = myFile.ContentLength;
-
-
//<snip> do some checks to see if it's accepted file type etc </snip>
-
-
-
// Read file into a data stream
-
byte[] myData = new Byte[iFileLen];
-
myFile.InputStream.Read(myData,0,iFileLen);
-
-
// Save the stream to disk
-
System.IO.FileStream newFile = new System.IO.FileStream(savepath, System.IO.FileMode.Create);
-
try
-
{
-
newFile.Write(myData,0, myData.Length);
-
}
-
catch{}
-
-
//anything i can think of to clear the file lock
-
inputFile.Dispose();
-
newFile.Flush();
-
newFile.Close();
-
newFile = null;
-
myFile = null;
-
GC.Collect();
-
-
-
if(createThumb)
-
{
-
-
string[] pth = savepath.Split('.');
-
string thumbpath = "";
-
for(int i=0;i<pth.Length-1;i++)
-
thumbpath += pth[i];
-
thumbpath += "_thumb";
-
thumbpath += pth[pth.Length-1];
-
-
createThumbnail(savepath,thumbpath,150,150);
-
-
}
-
}
-
}
-
-
-
-
-
public static void createThumbnail(string FullImagePath, string thumbnailPath, int maxWidth, int maxHeight)
-
{
-
//read the full size image -- ***This is where the exception is thrown***
-
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(FullImagePath);
-
-
// check the sizes to see if we need to resize
-
bool resize = false;
-
int thumbWidth = fullSizeImg.Width;
-
int thumbHeight = fullSizeImg.Height;
-
-
-
//check to see if the width is too large, set new sizes if required
-
if(thumbWidth > maxWidth)
-
{
-
resize = true;
-
thumbWidth = maxWidth;
-
thumbHeight = maxWidth/fullSizeImg.Width * fullSizeImg.Height;
-
}
-
-
// then we have to check to see if the height is within the constriants and try again
-
if(thumbHeight > maxHeight)
-
{
-
resize = true;
-
thumbHeight = maxHeight;
-
thumbWidth = maxHeight/fullSizeImg.Height * fullSizeImg.Width;
-
}
-
-
if(resize)
-
{
-
System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
-
thumbNailImg.Save(thumbnailPath,ImageFormat.Jpeg);
-
-
//Clean up / Dispose...
-
thumbNailImg.Dispose();
-
}
-
}
-
1 1552 Plater 7,872
Recognized Expert Expert
Well I'm not sure I'm following what you're doing with htmlinputfile and htmlpostfile, but it looks like you're closing all your filehandles and everything correctly.
I guess I would say that when reading an image in, I recomend using the Bitmap class (even if it's a jpeg/gif/png/whatever don't worry about it) it might have a better ability to input the file?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Pekka Niiranen |
last post by:
Hi,
I have used the following example from win32 extensions:
-----SCRIPT STARTS----
import win32file
import win32con
import win32security
import pywintypes
|
by: Stephen Corey |
last post by:
I'm writing an app that basically just appends text to a text file on a
Win2K Server. They fill out a form, click a button, and 1 line is
appended to the file. Multiple people will run this app at the same
time, and all will write to the same file. If I do an immediate flush()
on the file after writing the line, is there still a risk that 2
simultaneous writes will collide? If so, what's the best way to handle
this type of file write?
...
|
by: martin |
last post by:
Hi,
I have noticed that every aspx page that I created (and ascx file) has an
assosiated resource file aspx.resx.
However what I would like to do is have a single global resource file for
the site.
The resources in this global resource file will possibly change quite often,
there I guess this rules out using web.config as every time web.config
alters the application is recompiled, also I am not sure of the possible
consequences of...
|
by: Gary Nelson |
last post by:
Anyone have any idea why this code does not work?
FileOpen(1, "c:\JUNK\MYTEST.TXT", OpenMode.Binary,
OpenAccess.ReadWrite, OpenShare.Shared)
Dim X As Integer
For X = 1 To 26
FilePut(1, Chr(X + 64))
Next
Lock(1, 5, 10)
|
by: ABCL |
last post by:
Hi All,
I am working on the situation where 2 different
Process/Application(.net) tries to open file at the same time....Or one
process is updating the file and another process tries to access it,
it throws an exception.
How to solave this problem? So second process can wait until first
process completes its processing on the file.
Thanks in advance
| |
by: George |
last post by:
Hi,
I am re-writing part of my application using C#. This application starts
another process which execute a "legacy" program. This legacy program writes
to a log file and before it ends, it writes a specific string to the log file.
My original program (MKS Toolkit shell program) which keeps running "grep"
checking the "exit string" on the "log files". There are no file sharing
problem.
|
by: Luckbox72 |
last post by:
I am tring to open a compressed file with openfiledialog.
Private Function openfile() As String
Dim myFileName As String
Dim myLoadDialog As New OpenFileDialog
myLoadDialog.Filter = "zip|*.zip"
If myLoadDialog.ShowDialog() = DialogResult.OK Then
myFileName = myLoadDialog.FileName
Else
|
by: pgdown |
last post by:
Hi,
I have several processes accessing files from one folder, but only
one process should ever access each file. Once one process has the
file, no other process should be allowed to access it, even after the
first process is finished with it, except in the case where the first
process crashes. In pseudo code..
* Open file with exclusive lock
* Process file - failure will throw exception, skipping 'Remove
|
by: WingSiu |
last post by:
I am writing a Logging util for my ASP.NET application.
I am facing mulit process problem.
I developed a class LogFactory, and have a method called Get_Logger to
create a FileLogger, which will write text into a text file.
// sample code
FileLogger logger = LogFactory.Get_Logger();
logger.Log("Message here");
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |