Запись на изображение, чтобы преобразовать изображение только для чтения - PullRequest
0 голосов
/ 23 марта 2012

Я написал обработчик и функцию для отправки ответа с водяным знаком.Но каким-то образом изображение меняется только на чтение.

Я удалил весь объект.

Коды указаны ниже:

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;
}

Я что-то упустил?

Ответы [ 2 ]

1 голос
/ 23 марта 2012

если у вас возникла исключительная ситуация перед вызовом Dispose, ваши изображения не будут удалены, а файлы останутся заблокированными.Вы должны поместить вызовы Dispose в раздел «finally» или использовать «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 );
0 голосов
/ 03 мая 2012

Я решил свою проблему.Я не знаю, почему это работает, но это работает.

 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;
        }

...