.NETGURU
Bitmap object leaves file locked in ASP.NET
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-drawing' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Michael
-- Copied from [aspngfreeforall] to [ngfx-drawing] by Charles M. Carroll <Click here to reveal e-mail address> --Sorry for the repost I forgot the subject...I'm using Release candidate 0... so this problem may already be resolvedThe following code illustrates the problem...protected System.Web.UI.WebControls.Image Image1;System.Drawing.Bitmap myBitmap;private void Page_Load(object sender, System.EventArgs e){ // Put user code to initialize the page here String homeDir = this.MapPath("pics"); String filename = homeDir + "/test.bmp"; // Construct a bitmap myBitmap = new System.Drawing.Bitmap(filename); // Save it myBitmap.Save(homeDir + "/test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); // Dispose the image.... this free's up the filehandle in a windowsapplication myBitmap.Dispose(); Image1.ImageUrl = "pics/test.jpg";}The test.bmp file used to construct the Bitmap object remains locked afternot onlyafter exiting Page_Load, but also after the page has been closed. I haveseen itsomehow unlock itself... but the only sure way to unlock it is to reboot theserver.To those trying to repeat this problem I'd ask you try at least 3 times withadifferent bmp filename on each occasion.I've tested the same code within a windows application and the problem doesnot occur.This code may look a little silly but it's the simplest code I could comeup with to repeat the problem. The original code I encountered the problemwith is a little more complicated... and a little less silly.Has anyone else has encountered this problem ?Does anyone know a work around ?I would very much like to know if this occurs or doesn't occur in thereleased productthanks...Michael LangMS.NET Developerwww.michaellang.com.au
Reply to this message...
 
    
Kloberg Mac (LAM) (VIP)
Michael,
I've seen this too in Beta2 and the RC0 PDC release. I don't know if they
changed it in the RTM release (I purposely don't say 'fixed', because this
may be by design & there may be a good reason for this).

As you described, when somebody opens an Image object (goes for Bitmap too,
since it derives from Image), is seems to keep a lock on
the original file until the object is disposed. Setting the object to
nothing doesn't release the file, it must either be specifically disposed or
the garbage collector must collect it at some point later in time. The
garbage collector doesn't collect it immediately after your page goes out of
scope, but merely 'when it gets around to it'. This explains why you've seen
it unlock itself sometimes.

Two possible workarounds come to mind (#2 is probably the preferred one):

1. Work with a secondary image object and dispose the first one right away:

    oImage1.LoadFromFile(FilePath)
    oImage2 = CType(oImage.Clone, Image)
    oImage1.Dispose
    'Work with oImage2 from here
    
2. Use a buffered stream to load the image

    oFileStream = New FileStream(FilePath, FileMode.Open)
    oImage1 = oImage.FromStream(oFileStream)
    oFileStream.Close()
    oFileStream = Nothing
    
hth
--
Sincerely,
Mac Kloberg
LIEBHERR AMERICA INC.
http://www.liebherr-us.com

-----Original Message-----
From: Michael [mailto:Click here to reveal e-mail address]
Sent: Saturday, January 26, 2002 12:35 PM
To: ngfx-drawing
Subject: [ngfx-drawing] Bitmap object leaves file locked in ASP.NET

-- Copied from [aspngfreeforall] to [ngfx-drawing] by Charles M. Carroll
<Click here to reveal e-mail address> --

Sorry for the repost I forgot the subject...

I'm using Release candidate 0... so this problem may already be resolved
The following code illustrates the problem...

protected System.Web.UI.WebControls.Image Image1;
System.Drawing.Bitmap myBitmap;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
String homeDir = this.MapPath("pics");
String filename = homeDir + "/test.bmp";

// Construct a bitmap
myBitmap = new System.Drawing.Bitmap(filename);

// Save it
myBitmap.Save(homeDir + "/test.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);

// Dispose the image.... this free's up the filehandle in a windows
application
myBitmap.Dispose();

Image1.ImageUrl = "pics/test.jpg";
}

The test.bmp file used to construct the Bitmap object remains locked after
not only
after exiting Page_Load, but also after the page has been closed. I have
seen it
somehow unlock itself... but the only sure way to unlock it is to reboot the
server.
To those trying to repeat this problem I'd ask you try at least 3 times with
a
different bmp filename on each occasion.

I've tested the same code within a windows application and the problem does
not occur.

This code may look a little silly but it's the simplest code I could come
up with to repeat the problem. The original code I encountered the problem
with is a little more complicated... and a little less silly.

Has anyone else has encountered this problem ?
Does anyone know a work around ?
I would very much like to know if this occurs or doesn't occur in the
released product

thanks...

Michael Lang
MS.NET Developer
www.michaellang.com.au

| [ngfx-drawing] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-drawing.asp = JOIN/QUIT

Reply to this message...
 
    
Michael
Thanks so much Mac....

I thought I was already doing option 1 (with no success)...
Though I was only calling the Bitmap's Dispose methodand not setting it to
null... (naughty me I guess)

Option 2 however seems to work a treat !
(thank the Lord)

I actually had come up with an option 3 which involved loading the bitmap
via the C++ GDI api using a managed C++ library.... but this keeps it all C#
which (if u have ever tried writting managed C++ libraries u'll know) is a
great relief...

Thanks once again

Michael

[Original message clipped]

Reply to this message...
 
 
System.Drawing.Bitmap
System.Drawing.Imaging.ImageFormat
System.EventArgs
System.IO.FileMode
System.IO.FileStream
System.Web.UI.WebControls.Image




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification