IO Exception Thrown(进程无法访问文件'filename',因为它正被另一个进程使用 [英] IO Exception Thrown (The process cannot access the file 'filename' because it is being used by another process

查看:86
本文介绍了IO Exception Thrown(进程无法访问文件'filename',因为它正被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从我的服务器路径中删除图像文件它会导致进程无法访问文件FileName的错误,因为它正被另一个进程使用。我尝试了很多方法,但仍然徒劳无功。请帮我解决这个问题。



这是我的代码片段。

watermark();

DeleteImages();





public void DeleteImages()



{



试试

{



File.Delete(Server.MapPath(。\ \TmpImages\\WaterMark.jpg)); \\This Image Deleted Fine。

File.Delete(Server.MapPath(。\\TmpImages\\SavedImage.jpg)); \\删除此图片时抛出异常。

}



catch(例外情况)

{

LogManager.LogException(例如,删除图像时出错。);

Master.ShowMessage(ex.Message,true);

}



}





public void watermark()

{



//创建一个包含水印照片的图像对象

Image imgPhoto = Image.FromFile(Server .MapPath(。\\TmpImages\\SavedImage.jpg));

int phWidth = imgPhoto.Width;

int phHeight = imgPhoto.Height ;



//创建原始照片大小的位图

位图bmPhoto =新位图(phWidth,phHeight,Pixe lFormat.Format24bppRgb);



bmPhoto.SetResolution(imgPhoto.Horizo​​ntalResolution,imgPhoto.VerticalResolution);



//将Bitmap加载到Graphics对象中

图形grPhoto = Graphics.FromImage(bmPhoto);



//创建一个包含图像的对象水印

Image imgWatermark = new Bitmap(Server.MapPath(。\\TmpImages\\PrintasWatermark.jpg));

int wmWidth = imgWatermark .Width;

int wmHeight = imgWatermark.Height;



//设置此Graphics对象的渲染质量

grPhoto.SmoothingMode = SmoothingMode.AntiAlias;



//将原始大小的照片Image对象绘制到图形对象。

grPhoto .DrawImage(

imgPhoto,//照片图片对象

new Rec tangle(0,0,phWidth,phHeight),//矩形结构

0,//要绘制的源图像部分的x坐标。

0, //要绘制的源图像部分的y坐标。

phWidth,//要绘制的源图像部分的宽度。

phHeight,//要绘制的源图像部分的高度。

GraphicsUnit.Pixel); //计量单位



// --------------------------- ----------------------------

//最大化版权信息的大小我们将

//测试多个字体大小以确定最大可能

//我们可以用于照片宽度的字体

//定义一个数组您希望考虑的点尺寸作为可能性

// ------------------------------ -------------------------





/ /通过将文本对齐设置为居中来定义文本布局

StringFormat StrFormat = new StringFormat();

StrFormat.Alignment = StringAlignment.Center;



//定义一个半透明黑色的画笔(Alpha设置为153)

SolidBrush semiTransBrush2 =新的SolidBrush(Color.FromArgb(153,0,0, 0));







//定义一个半透明白色的画笔(Alpha设置为153)

SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153,255,255,255));







// ---------------------- --------------------------------------

// Step# 2 - 插入水印图像

// ----------------------------------- -------------------------



//根据以前的内容创建一个位图修改后的照片位图

位图bmWatermark =新位图(bmPhoto);

bmWatermark.SetResolution(imgPhoto.Horizo​​ntalResolution,imgPhoto.VerticalResolution);

/ /将此位图加载到新的图形对象中

图形grWatermark = Graphics.FromImage(bmWatermark);



//实现transulcent水印我们将通过定义一个ImageAttributes对象来应用(2)颜色

//操作/>
// seting(2)其属性。

ImageAttributes imageAttributes = new ImageAttributes();



//操作水印图像的第一步是将背景颜色替换为背景颜色(Alpha = 0,R = 0,G = 0,B = 0)

//背景颜色
//为此我们将使用Colormap并使用它来定义RemapTable

ColorMap colorMap = new ColorMap();



//我的水印定义为100%绿色背景这将是我们搜索并替换为透明度的颜色

colorMap.OldColor = Color.FromArgb(255,0,255,0);

colorMap.NewColor = Color.FromArgb(0,0,0,0);



ColorMap [] remapTable = {colorMap};



imageAttributes.SetRemapTable(remapTable,ColorAdjustType.Bitmap);



//第二个颜色的男人ipulation用于改变

//水印的不透明度。这是通过应用包含RGBA空间的

//坐标的5x5矩阵来完成的。通过将第3行和第3列

//设置为0.3f,我们达到了不透明度水平

float [] [] colorMatrixElements = {

new float [] {1.0f,0.0f,0.0f,0.0f,0.0f},

new float [] {0.0f,1.0f,0.0f,0.0f,0.0f} ,

new float [] {0.0f,0.0f,1.0f,0.0f,0.0f},

new float [] {0.0f,0.0f,0.0 f,0.3f,0.0f},

new float [] {0.0f,0.0f,0.0f,0.0f,1.0f}};

ColorMatrix wmColorMatrix =新的ColorMatrix(colorMatrixElements);



imageAttributes.SetColorMatrix(wmColorMatrix,ColorMatrixFlag.Default,

ColorAdjustType.Bitmap);



//对于这个例子我们w生病了右上方的水印

//照片的手角。向下偏移10像素到

//留下10个像素



int xPosOfWm =((phWidth - wmWidth) - 10);

int yPosOfWm = 10;



grWatermark.DrawImage(imgWatermark,

new Rectangle(xPosOfWm,yPosOfWm,wmWidth) ,wmHeight),//设置排序位置

0,//要绘制的源图像部分的x坐标。

0,// y坐标要绘制的源图像部分。

wmWidth,//水印宽​​度

wmHeight,//水印高度

GraphicsUnit.Pixel, //测量单位

imageAttributes); // ImageAttributes对象



//用新的位图替换原始的photgraphs位图

imgPhoto = bmWatermark;

grPhoto.Dispose();

grWatermark.Dispose();

//将新图像保存到文件系统。

imgPhoto.Save( Server.MapPath(。\\TmpImages\\WaterMark.jpg),ImageFormat.Jpeg);

imgPhoto.Dispose();

imgWatermark。 Dispose();





}

解决方案

该文件可能是在另一个程序中打开。

确保它已关闭,没有其他进程正在访问此文件。


这是服务器操作系统的工作,以防止您删除另一个进程中正在使用的文件。



唯一的解决方案是让另一个进程关闭文件或关闭该进程。当文件被释放时,您将能够删除它。


不要只是加载图像 - 文档确实说文件或流必须在图像实例。

相反,从文件中加载它,从中创建一个新图像,然后处理原始文件:

图像imgPhoto = GetCopyImage(Server.MapPath( 。\\TmpImages\\SavedImage.jpg)); 
...
private 图片GetCopyImage( string 路径)
{
使用(图片im = Image.FromFile(路径))
{
位图bm = 位图(im);
return bm;
}
}

如果您只是加载它,那么图像引用将保持文件打开,直到它被处理 - 这可能不会发生,直到应用程序关闭。


I am Unable To Delete Image File From My Server Path It Gaves Error That The Process Cannot Access The File "FileName" Because it is being Used By Another Process. I Tried Many Methods But Still All In Vain. Please Help me Out in This Issue.

Here is My Code Snippet.
watermark();
DeleteImages();


public void DeleteImages()

{

try
{

File.Delete(Server.MapPath(".\\TmpImages\\WaterMark.jpg")); \\This Image Deleted Fine.
File.Delete(Server.MapPath(".\\TmpImages\\SavedImage.jpg")); \\ Exception Thrown On Deleting of This Image.
}

catch (Exception ex)
{
LogManager.LogException(ex, "Error in Deleting Images.");
Master.ShowMessage(ex.Message, true);
}

}


public void watermark()
{

//create a image object containing the photograph to watermark
Image imgPhoto = Image.FromFile(Server.MapPath(".\\TmpImages\\SavedImage.jpg"));
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;

//create a Bitmap the Size of the original photograph
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

//load the Bitmap into a Graphics object
Graphics grPhoto = Graphics.FromImage(bmPhoto);

//create a image object containing the watermark
Image imgWatermark = new Bitmap(Server.MapPath(".\\TmpImages\\PrintasWatermark.jpg"));
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;

//Set the rendering quality for this Graphics object
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

//Draws the photo Image object at original size to the graphics object.
grPhoto.DrawImage(
imgPhoto, // Photo Image object
new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
phWidth, // Width of the portion of the source image to draw.
phHeight, // Height of the portion of the source image to draw.
GraphicsUnit.Pixel); // Units of measure

//-------------------------------------------------------
//to maximize the size of the Copyright message we will
//test multiple Font sizes to determine the largest posible
//font we can use for the width of the Photograph
//define an array of point sizes you would like to consider as possiblities
//-------------------------------------------------------


//Define the text layout by setting the text alignment to centered
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;

//define a Brush which is semi trasparent black (Alpha set to 153)
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));



//define a Brush which is semi trasparent white (Alpha set to 153)
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));



//------------------------------------------------------------
//Step #2 - Insert Watermark image
//------------------------------------------------------------

//Create a Bitmap based on the previously modified photograph Bitmap
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Load this Bitmap into a new Graphic Object
Graphics grWatermark = Graphics.FromImage(bmWatermark);

//To achieve a transulcent watermark we will apply (2) color
//manipulations by defineing a ImageAttributes object and
//seting (2) of its properties.
ImageAttributes imageAttributes = new ImageAttributes();

//The first step in manipulating the watermark image is to replace
//the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
//to do this we will use a Colormap and use this to define a RemapTable
ColorMap colorMap = new ColorMap();

//My watermark was defined with a background of 100% Green this will
//be the color we search for and replace with transparency
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

ColorMap[] remapTable = { colorMap };

imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

//The second color manipulation is used to change the opacity of the
//watermark. This is done by applying a 5x5 matrix that contains the
//coordinates for the RGBA space. By setting the 3rd row and 3rd column
//to 0.3f we achive a level of opacity
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);

//For this example we will place the watermark in the upper right
//hand corner of the photograph. offset down 10 pixels and to the
//left 10 pixles

int xPosOfWm = ((phWidth - wmWidth) - 10);
int yPosOfWm = 10;

grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
wmWidth, // Watermark Width
wmHeight, // Watermark Height
GraphicsUnit.Pixel, // Unit of measurment
imageAttributes); //ImageAttributes Object

//Replace the original photgraphs bitmap with the new Bitmap
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
//save new image to file system.
imgPhoto.Save(Server.MapPath(".\\TmpImages\\WaterMark.jpg"), ImageFormat.Jpeg);
imgPhoto.Dispose();
imgWatermark.Dispose();


}

解决方案

The file is probably open in another program.
Make sure it is closed and no other process is accessing this file.


This is the job of the server OS to prevent you from deleting a file that is in use in another process.

The only solution is to have the other process to close the file or to close that process. As the file will be freed, you will be able to delete it.


Don't just load the image - the documentation does say that the file or stream must be available for the duration of the Image instance.
Instead, load it from the file, create a new image from it, and then dispose the original:

    Image imgPhoto = GetCopyImage(Server.MapPath(".\\TmpImages\\SavedImage.jpg"));
    ... 
private Image GetCopyImage(string path)
    {
    using (Image im = Image.FromFile(path))
        {
        Bitmap bm = new Bitmap(im);
        return bm;
        }
    }

If you just load it, then the image reference will hold the file open until it is Disposed - which may not happen until the application is closed.


这篇关于IO Exception Thrown(进程无法访问文件'filename',因为它正被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆