webForumDet fria alternativet

Bild som är låst.

.NETur .NET

2 svar · 388 visningar · startad av icaaq

icaaqMedlem sedan okt. 20005 273 inlägg
#1

jag har en upload som fungerar finfint. Men sen har jag en HttpHandler som presenterar bilden. Och om jag har presenterat bilden med den och sedan försöker ladda upp en ny bild med samma namn så får jag.

The process cannot access the file "F:\Inetpub\wwwroot\wF\personal\img\6.jpg" because it is being used by another process

Jag förstår att filen används av httpHandlern men jag kan inte se var jag ska stänga den, så här kommer koden.

[red]
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using NineteenDecember;

namespace NineteenDecember.HttpHandler
{
	/// <summary>
	/// Summary description for viewimage.
	/// </summary>
	public class ViewImage: IHttpHandler
	{
		public void ProcessRequest(HttpContext context)
		{
			Image im = null;
			string imgPath = "img/vforallaheader.jpg";			
			int imgQuality = 80;
			if(HttpContext.Current.Request.QueryString["Path"] != null)
				imgPath = HttpContext.Current.Request.QueryString["Path"];
			if(HttpContext.Current.Request.QueryString["Quality"] != null)
				imgQuality = Convert.ToInt32(HttpContext.Current.Request.QueryString["Quality"]);
			if(!System.IO.File.Exists(HttpContext.Current.Server.MapPath(imgPath)))
			{
				im = Image.FromFile(HttpContext.Current.Server.MapPath("img/vforallaheader.jpg"));
			}
			else
			{
				try
				{
					im = Image.FromFile(HttpContext.Current.Server.MapPath(imgPath));
				}
				catch( Exception ex )
				{ 
					im = Image.FromFile(HttpContext.Current.Server.MapPath("img/vforallaheader.jpg"));
				}
			}
			int imgSize = (int)im.Width;
			if(HttpContext.Current.Request.QueryString["Size"] != null)
			{
				imgSize = Convert.ToInt32(HttpContext.Current.Request.QueryString["Size"]);
			}
			MemoryStream ms = Upload.ScaleAndStream(HttpContext.Current.Server.MapPath(imgPath), imgSize, imgQuality);
			Byte[] Buffer = ms.ToArray();
			HttpContext.Current.Response.BufferOutput = true;
			ms.Close();
			im.Dispose();
			try
			{
				HttpContext.Current.Response.ClearContent();
				HttpContext.Current.Response.ClearHeaders();
				HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
				HttpContext.Current.Response.ContentType = "Image/jpeg";
				HttpContext.Current.Response.AppendHeader("content-disposition", "inline");
				HttpContext.Current.Response.OutputStream.Write(Buffer, 0, Buffer.Length);
			}
			catch( Exception ex )				
			{ 
				//Debug.WriteLine(inputFileName);
				throw(ex); 
			}
		}
		public bool IsReusable 
		{
			get
			{
				return true;
			}
		}        
	}
}
[/red]

tacksamt om ni kunde hjälpa mig att felsöka denna....

mv icaaq

fredrikMedlem sedan dec. 19991 072 inlägg
#2

På vilken rad får du felet?

Det är alltid viktigt att köra "Dispose" på image-objektet så fort man är klar med det. Om du ska använda samma fil 2 gånger efter vartannat kan det vara läge att göra. GC.Collect() så att bilden verkligen rensas bort. Har själv haft liknande problem, men då har .Collect() löst problemet.

icaaqMedlem sedan okt. 20005 273 inlägg
#3
[red]
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using NineteenDecember;

namespace NineteenDecember.HttpHandler
{
	/// <summary>
	/// Summary description for viewimage.
	/// </summary>
	public class ViewImage: IHttpHandler
	{
		public void ProcessRequest(HttpContext context)
		{
			Image im = null;
			string imgPath = "img/vforallaheader.jpg";			
			int imgQuality = 80;
			if(HttpContext.Current.Request.QueryString["Path"] != null)
				imgPath = HttpContext.Current.Request.QueryString["Path"];
			if(HttpContext.Current.Request.QueryString["Quality"] != null)
				imgQuality = Convert.ToInt32(HttpContext.Current.Request.QueryString["Quality"]);
			if(!System.IO.File.Exists(HttpContext.Current.Server.MapPath(imgPath)))
			{
				im = Image.FromFile(HttpContext.Current.Server.MapPath("img/vforallaheader.jpg"));
			}
			else
			{
				try
				{
					im = Image.FromFile(HttpContext.Current.Server.MapPath(imgPath));
				}
				catch( Exception ex )
				{ 
					im = Image.FromFile(HttpContext.Current.Server.MapPath("img/vforallaheader.jpg"));
				}
			}
			int imgSize = (int)im.Width;
			if(HttpContext.Current.Request.QueryString["Size"] != null)
			{
				imgSize = Convert.ToInt32(HttpContext.Current.Request.QueryString["Size"]);
			}
			MemoryStream ms = Upload.ScaleAndStream(HttpContext.Current.Server.MapPath(imgPath), imgSize, imgQuality);
			Byte[] Buffer = ms.ToArray();
			HttpContext.Current.Response.BufferOutput = true;
			ms.Close();
			im.Dispose();
			try
			{
				HttpContext.Current.Response.ClearContent();
				HttpContext.Current.Response.ClearHeaders();
				HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
				HttpContext.Current.Response.ContentType = "Image/jpeg";
				HttpContext.Current.Response.AppendHeader("content-disposition", "inline");
				HttpContext.Current.Response.OutputStream.Write(Buffer, 0, Buffer.Length);
			}
			catch( Exception ex )				
			{ 
				//Debug.WriteLine(inputFileName);
				throw(ex); 
			}
			[b]finally
			{
				GC.Collect();
			}[/b]
		}
		public bool IsReusable 
		{
			get
			{
				return true;
			}
		}        
	}
}[/red]

mv icaaq

136 ms totalt · 3 externa anrop · v20260731065814-full.29ac60f6
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
133 ms — hämta tråd, inlägg och bilagor (db)