我编写了一个处理程序和一个函数来发送水印响应。但有些图像如何变为只读。
我处理了所有物品。
代码如下:
public void ProcessRequest(HttpContext context)
{
var imagePath = QueryString.getValueOf("ID");
var watermark = QueryString.getValueOf("watermark");
context.Response.ContentType = "image/jpeg";
if (string.IsNullOrWhiteSpace(imagePath) || string.IsNullOrWhiteSpace(watermark))
{
var originalImage = Image.FromFile(context.Server.MapPath("Images/NoImage.jpg"));
originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
originalImage.Dispose();
}
else
{
if (watermark == "0")
{
var absolutePath = context.Server.MapPath(imagePath);
var fileexist = System.IO.File.Exists(absolutePath);
if (fileexist)
{
var originalImage = Image.FromFile(absolutePath);
originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
originalImage.Dispose();
}
else
{
var originalImage = Image.FromFile(context.Server.MapPath("Images/NoImage.jpg"));
originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
originalImage.Dispose();
}
}
else
{
if (imagePath.ToUpper().Contains(".GIF"))
{
var absolutePath = context.Server.MapPath(imagePath);
var fileexist = System.IO.File.Exists(absolutePath);
if (fileexist)
{
var originalImage = Image.FromFile(absolutePath);
originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
originalImage.Dispose();
}
else
{
var originalImage = Image.FromFile(context.Server.MapPath("Images/NoImage.jpg"));
originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
originalImage.Dispose();
}
}
else
{
var absolutePath = context.Server.MapPath(imagePath);
var fileexist = System.IO.File.Exists(absolutePath);
if (fileexist)
{
var originalImage = Image.FromFile(absolutePath);
originalImage = new ImageMethods().AddWatermarkText(originalImage);
originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
originalImage.Dispose();
}
else
{
var originalImage = Image.FromFile(context.Server.MapPath("Images/NoImage.jpg"));
originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
originalImage.Dispose();
}
}
}
}
}
并且功能是写图像如下
public Image AddWatermarkText(Image img)
{
try
{
var textOnImage = ConfigurationManager.AppSettings["textOnImage"];
var opacity = Int32.Parse(ConfigurationManager.AppSettings["opicity"]);
var red = Int32.Parse(ConfigurationManager.AppSettings["red"]);
var green = Int32.Parse(ConfigurationManager.AppSettings["green"]);
var blue = Int32.Parse(ConfigurationManager.AppSettings["blue"]);
var fontSize = Int32.Parse(ConfigurationManager.AppSettings["fontSize"]);
var fontName = ConfigurationManager.AppSettings["fontName"];
var lobFromImage = Graphics.FromImage(img);
var lobFont = new Font(fontName, fontSize, FontStyle.Regular);
var lintTextHw = lobFromImage.MeasureString(textOnImage, lobFont);
var lintTextOnImageWidth = (int)lintTextHw.Width;
var lintTextOnImageHeight = (int)lintTextHw.Height;
var lobSolidBrush = new SolidBrush(Color.FromArgb(opacity, Color.FromArgb(red, green, blue)));
var posLeft = (img.Width - lintTextOnImageWidth) / 2;
posLeft = posLeft > 0 ? posLeft : 5;
var lobPoint = new Point(posLeft, (img.Height / 2) - (lintTextOnImageHeight / 2));
// var lobPoint = new Point(RandomNumber(0, img.Width - lintTextOnImageWidth), RandomNumber(0, img.Height - lintTextOnImageHeight));
lobFromImage.DrawString(textOnImage, lobFont, lobSolidBrush, lobPoint);
lobFromImage.Dispose();
lobSolidBrush.Dispose();
lobFont.Dispose();
}
catch (Exception ex)
{
HavException = true;
ExceptionMessage = ex.Message;
}
return img;
}
我错过了什么吗?
答案 0 :(得分:1)
如果您在Dispose调用之前有异常,那么您的图像将不会被处理,文件将保持锁定状态。您应该在“finally”部分中调用Dispose或使用“using”,如:
using (var originalImage = Image.FromFile(absolutePath))
originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
另外,为避免锁定,您可以先读取文件流,然后从流
创建图像using(var imageStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
return Image.FromStream(imageStream );
答案 1 :(得分:0)
我解决了我的问题。我不知道为什么它有效但它有效。
public void ProcessRequest(HttpContext context) { var imagePath = QueryString.getValueOf("ID"); var watermark = QueryString.getValueOf("watermark"); string lstrResponseType; // context.Response.ContentType = "image/jpeg"; if (string.IsNullOrWhiteSpace(imagePath) || string.IsNullOrWhiteSpace(watermark)) { using (var originalImage = Image.FromFile(context.Server.MapPath("Images/NoImage.jpg"))) { originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg); originalImage.Dispose(); } lstrResponseType = "image/jpeg"; } else { var absolutePath = context.Server.MapPath(imagePath); var fileexist = System.IO.File.Exists(absolutePath); if (!fileexist) { var originalImage = Image.FromFile(context.Server.MapPath("Images/NoImage.jpg")); originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg); lstrResponseType = "image/jpeg"; originalImage.Dispose(); } else { using (var originalImage = Image.FromFile(absolutePath)) { if (watermark == "0") { originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg); lstrResponseType = "image/jpeg"; } else { if (imagePath.ToUpper().Contains(".GIF")) { originalImage.Save(context.Response.OutputStream, ImageFormat.Gif); lstrResponseType = "image/gif"; } else { new ImageMethods().AddWatermarkText(originalImage); originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg); lstrResponseType = "image/jpeg"; } } } } } context.Response.ContentType = lstrResponseType; }