이미지의 크기를 조정하려고합니다. 첫째로 나는 바이트 배열로 이미지를 읽어 메모리 크기를 조정하고 같은 파일을 다시 쓰기 :동일한 프로세스에 의해 잠긴 파일을 대체하려면 어떻게해야합니까?
public static void CropAndResizeImage(EntryImage image, int left, int top, int right, int bottom)
{
Size newSize = new Size();
string imagePathAndFilename = HttpContext.Current.Server.MapPath(image.URL);
//byte[] photoBytes = File.ReadAllBytes(imagePathAndFilename);
using (FileStream fs = new FileStream(imagePathAndFilename, FileMode.Open, FileAccess.ReadWrite))
{
fs.Position = 0;
var photoBytes = new byte[fs.Length];
int read = fs.Read(photoBytes, 0, photoBytes.Length);
// Process photo and resize
using (MemoryStream inStream = new MemoryStream(photoBytes))
using (MemoryStream outStream = new MemoryStream())
{
using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))// Initialize the ImageFactory using the overload to preserve EXIF metadata.
{
ISupportedImageFormat format = new JpegFormat { Quality = 75 }; // Format is automatically detected though can be changed.
Size maxSize = new Size(1024, 1024);
ResizeLayer layer = new ResizeLayer(maxSize, upscale: false, resizeMode: ResizeMode.Max);
layer.Upscale = false;
// Load, resize, set the format and quality and save an image.
imageFactory.Load(inStream)
.Crop(new CropLayer(left, top, right - left, bottom - top, CropMode.Pixels)) // Crop is relative to image edge, not absolute coords.
.Resize(layer)
.Format(format)
.Save(outStream);
newSize.Width = imageFactory.Image.Width;
newSize.Height = imageFactory.Image.Height;
}
// Write back to the same file
fs.Position = 0;
fs.SetLength(photoBytes.Length);
fs.Write(photoBytes, 0, photoBytes.Length);
}
}
}
그러나 일반적으로 다음과 같은 오류 얻을 :
The process cannot access the file: 'C:\folder\image.jpg' because it is being used by another process.
이 왜입니까? 나는 File.ReadAllBytes()가 파일을 자동으로 닫을 것이라고 추정했을 것이다.
프로세스 탐색기에서 파일에 대한 파일 핸들이나 잠금이 표시되지 않습니다 (이상하게 보임). 전체 코드를 표시하도록 업데이트 내 코드에 정비공의 답변을 통합 :
bool saved = false;
while (!saved)
{
try
{
SaveImageToFile(imagePathAndFilename, outStream);
saved = true;
}
catch (IOException ex)
{
System.Threading.Thread.Sleep(1000);
}
}
편집 :
은 내가 while 루프에서 약간의 지연을 추가하더라도, 루프 파일을 의미하는 것은 영구적으로 고정되지 않은이 완료 결코 내 구현을 보여주는 위의.
그것을 그렇다면, 프로그램의 다른 부분, 예를 들어 코드의 'Bitmap.FromStream (imagePathAndFilename)'과 같은 파일을 만질 수 있습니까? 이 코드는 문제의 *** 원인 ***이되지는 않지만 문제의 영향을받을 수는 있지만 문제의 원인이 아닙니다. –
[docs] (https://msdn.microsoft.com/en-us/library/system.io.file.readallbytes (v = vs.110) .aspx) 및 [참조 원본] (http : /referencesource.microsoft.com/#mscorlib/system/io/file.cs,4b24188ee62795aa), 그렇지 않습니다. 당신의 문제는 아마도 다른 곳에있을 것입니다. –
파일을 읽는 코드가 없습니다. 이미지 크기를 조정하는 코드는 직접 photoBytes 바이트 배열에서 수행합니다. – NickG