D
D
Dmitry Filandor2015-07-31 10:02:21
ASP.NET
Dmitry Filandor, 2015-07-31 10:02:21

How to unlock a graphic file (grayscale conversion)?

Hello! There is a task - to convert uploaded files to gray tones, here's what I'm doing:

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

no matter how I tried, all the time an error occurred when deleting (in general, I will replace it with a gray one) of the original image:
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.
As I understand it, the blocking occurs in the MakeGrayscale3 method itself and is not removed until IIS is stopped

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Pukhov, 2015-07-31
@LifeAct

Stream s = System.IO.File.OpenRead(basePath + "\\" + fileName + ".jpg"
you opened the file for reading and then you are trying to delete it without closing it.
transfer System.IO.File.Delete(basePath + " \\" + fileName + ".jpg"); behind the closing bracket using

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question