Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question