LifeAct
@LifeAct
Создаем и раскручиваем, не ставим на конвейер

Как расблокировать графический файл (конвертация в серые тона)?

Всем привет! Есть задача - конвертировать загружаемые файлы в серые тона, вот что делаю:

....
 using (Stream s = System.IO.File.OpenRead(basePath + "\\" + fileName + ".jpg")) {

                System.Drawing.Bitmap normalImage = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(s);
                
                System.Drawing.Image grayScaled = (System.Drawing.Image)MakeGrayscale3(new System.Drawing.Bitmap(normalImage));
                grayScaled.Save(basePath + "\\" + fileName + "g.jpg");

                normalImage.Dispose();
                System.IO.File.Delete(basePath + "\\" + fileName + ".jpg");

               
            }

  public static System.Drawing.Bitmap MakeGrayscale3(System.Drawing.Bitmap original)
        {
            //create a blank bitmap the same size as original
            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(original.Width, original.Height);
            //get a graphics object from the new image
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newBitmap);
            //create the grayscale ColorMatrix
            System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
               new float[][]
              {
                 new float[] {.3f, .3f, .3f, 0, 0},
                 new float[] {.59f, .59f, .59f, 0, 0},
                 new float[] {.11f, .11f, .11f, 0, 0},
                 new float[] {0, 0, 0, 1, 0},
                 new float[] {0, 0, 0, 0, 1}
              });
            //create some image attributes
            System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
            //set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);
            //draw the original image on the new image
            //using the grayscale color matrix
            g.DrawImage(original, new System.Drawing.Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, System.Drawing.GraphicsUnit.Pixel, attributes);
            //dispose the Graphics object
            g.Dispose();

            original.Dispose();           

            return newBitmap;
        }


как не пытался, все время ошибка при удалении (вообще я ее буду серой заменять) оригинальной картинки:
System.IO.IOException
The process cannot access the file 'D:\Public\_SITES_\Images\USERDATA\Posts\07-2015\u2t394912.jpg' because it is being used by another process.

Я так понимаю блокировка происходит в самом методе MakeGrayscale3 и не снимается пока не остановить IIS
  • Вопрос задан
  • 185 просмотров
Решения вопроса 1
Neuroware
@Neuroware
Программист в свободное от работы время
Stream s = System.IO.File.OpenRead(basePath + "\\" + fileName + ".jpg"
вы открыли файл для чтения и потом пытаетесь его же удалить не закрыв.
перенесите System.IO.File.Delete(basePath + "\\" + fileName + ".jpg"); за закрывающую скобку using
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы