获得一个位图每当我试图让我从我得到的参数异常的情况下缓存的位图。位图是从缓存对象中转换而来的,但内容已损坏。从缓存
抛出异常就行
ImageFormat imageFormat = bitmap.RawFormat;
bitmap.RawFormat”扔类型的异常‘System.ArgumentException’
刚刚给我的消息‘参数无效’。
当我坚持在代码看看缓存中的所有属性报到相同的异常创建位图断点。
下面是从我的处理程序处理请求....
/// <summary>
/// Processes the image request.
/// </summary>
/// <param name="context">The httpContext handling the request.</param>
public void ProcessRequest(HttpContext context)
{
//write your handler implementation here.
if (!string.IsNullOrEmpty(context.Request.QueryString["file"]))
{
string file = context.Request.QueryString["file"];
bool thumbnail = Convert.ToBoolean(context.Request.QueryString["thumb"]);
// Throw in some code to check width and height.
int width = Convert.ToInt32(context.Request.QueryString["width"]);
int height = Convert.ToInt32(context.Request.QueryString["height"]);
// Store our context key.
string key = file;
// Strip away our cache data value.
file = file.Substring(0, file.LastIndexOf("_", StringComparison.OrdinalIgnoreCase));
OnServing(file);
try
{
//Check the cache for a file.
Bitmap bitmap = (Bitmap)context.Cache[key];
if (bitmap != null)
{
ImageFormat imageFormat = bitmap.RawFormat;
// We've found something so lets serve that.
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, imageFormat);
context.Response.BinaryWrite(ms.ToArray());
}
}
else
{
// Nothing found lets create a file.
// Lock the file to prevent access errors.
lock (this.syncRoot)
{
string path = HostingEnvironment.MapPath(String.Format("~/Images/{0}", file));
FileInfo fi = new FileInfo(path);
if (fi.Exists)
{
using (Bitmap img = (Bitmap)Bitmap.FromFile(path))
{
ImageFormat imageFormat = img.RawFormat;
if (thumbnail)
{
ImageEditor imageEditor = new ImageEditor(img);
Size size = new Size(width, height);
imageEditor.Resize(size, true);
imageEditor.FixGifColors();
using (MemoryStream ms = new MemoryStream())
{
imageEditor.Image.Save(ms, imageFormat);
// Add the file to the cache.
context.Cache.Insert(key, img, new System.Web.Caching.CacheDependency(path));
imageEditor.Dispose();
context.Response.BinaryWrite(ms.ToArray());
}
}
else
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, imageFormat);
// Add the file to the cache.
context.Cache.Insert(key, img, new System.Web.Caching.CacheDependency(path));
context.Response.BinaryWrite(ms.ToArray());
}
}
OnServed(file);
}
}
else
{
OnBadRequest(file);
}
}
}
}
catch (Exception ex)
{
throw ex;
// OnBadRequest(ex.Message);
// return a default empty file here.
}
}
}
任何帮助将不胜感激。
您能否发布异常的确切详情?它出现什么样的线路和异常的信息是什么? – 2010-10-18 10:14:43
我已经用异常详细信息更新了问题。 – 2010-10-18 10:25:15