C# 文件正忙。我能';不要删除它
我为这幅图像设置了版权。之后,我需要删除旧文件。但我不能,因为很忙。 我需要将受版权保护的文件从“受版权保护的”+imageFile重命名为imageFileC# 文件正忙。我能';不要删除它,c#,.net,file,C#,.net,File,我为这幅图像设置了版权。之后,我需要删除旧文件。但我不能,因为很忙。 我需要将受版权保护的文件从“受版权保护的”+imageFile重命名为imageFile public static void SetImageCopyright(string imageFile) { #region //create a image object containing the photograph to watermark
public static void SetImageCopyright(string imageFile)
{
#region
//create a image object containing the photograph to watermark
Image imgPhoto = Image.FromFile(imageFile);
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);
//------------------------------------------------------------
//Step #1 - Insert Copyright message
//------------------------------------------------------------
//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
//-------------------------------------------------------
int[] sizes = new int[] { 25, 16, 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
//Loop through the defined sizes checking the length of the Copyright string
//If its length in pixles is less then the image width choose this Font size.
for (int i = 0; i < 7; i++)
{
//set a Font object to Arial (i)pt, Bold
crFont = new Font("arial", sizes[i], FontStyle.Bold);
//Measure the Copyright string in this Font
crSize = grPhoto.MeasureString("AlexMaslakov.ru", crFont);
if ((ushort)crSize.Width < (ushort)phWidth)
break;
}
//Since all photographs will have varying heights, determine a
//position 5% from the bottom of the image
int yPixlesFromBottom = (int)(phHeight * .05);
//Now that we have a point size use the Copyrights string height
//to determine a y-coordinate to draw the string of the photograph
float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));
//Determine its x-coordinate by calculating the center of the width of the image
float xCenterOfImg = (phWidth / 2);
//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));
//Draw the Copyright string
//grPhoto.DrawString(Copyright, //string of text
// crFont, //font
// semiTransBrush2, //Brush
// new PointF(xCenterOfImg + 5, yPosFromBottom + 5), //Position
// StrFormat);
//define a Brush which is semi trasparent white (Alpha set to 153)
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
//Draw the Copyright string a second time to create a shadow effect
//Make sure to move this text 1 pixel to the right and down 1 pixel
grPhoto.DrawString("AlexMaslakov.ru", //string of text
crFont, //font
semiTransBrush, //Brush
new PointF(xCenterOfImg, yPosFromBottom), //Position
StrFormat); //Text alignment
//------------------------------------------------------------
//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);
#endregion
//Replace the original photgraphs bitmap with the new Bitmap
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
bmPhoto.Dispose();
//save new image to file system.
imgPhoto.Save("copyrighted_"+imageFile, ImageFormat.Jpeg);
imgPhoto.Dispose();
}
公共静态无效SetImageCopyright(字符串imageFile)
{
#区域
//创建包含要添加水印的照片的图像对象
Image imgPhoto=Image.FromFile(imageFile);
int phWidth=imgPhoto.Width;
int phHeight=imgPhoto.Height;
//创建原始照片大小的位图
位图bmPhoto=新位图(phWidth、phHeight、PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
//将位图加载到图形对象中
Graphics grPhoto=Graphics.FromImage(bmPhoto);
//------------------------------------------------------------
//步骤#1-插入版权信息
//------------------------------------------------------------
//设置此图形对象的渲染质量
grPhoto.SmoothingMode=SmoothingMode.AntiAlias;
//将原始大小的照片图像对象绘制到图形对象。
grPhoto.DrawImage(
imgPhoto,//照片图像对象
新矩形(0,0,phWidth,phHeight),//矩形结构
0,//要绘制的源图像部分的x坐标。
0,//要绘制的源图像部分的y坐标。
phWidth,//要绘制的源图像部分的宽度。
phHeight,//要绘制的源图像部分的高度。
GraphicsUnit.Pixel);//度量单位
//-------------------------------------------------------
//为了最大化版权信息的大小,我们将
//测试多个字体大小以确定最大可能的字体大小
//我们可以用来测量照片宽度的字体
/定义一个你想考虑的点大小数组
//-------------------------------------------------------
int[]大小=新的int[]{25,16,12,10,8,6,4};
Font crFont=null;
SizeF crSize=新的SizeF();
//循环检查定义的大小,检查版权字符串的长度
//如果其长度(以像素为单位)小于图像宽度,请选择此字体大小。
对于(int i=0;i<7;i++)
{
//将字体对象设置为Arial(i)pt,粗体
crFont=新字体(“arial”,大小[i],字体样式.粗体);
//测量此字体中的版权字符串
crSize=grPhoto.MeasureString(“AlexMaslakov.ru”,crFont);
如果((ushort)crSize.Width<(ushort)phWidth)
打破
}
//由于所有照片都有不同的高度,请确定
//距离图像底部5%的位置
int-yPixlesFromBottom=(int)(phHeight*.05);
//现在我们有了一个点大小,使用版权字符串高度
//确定用于绘制照片字符串的y坐标的步骤
浮动yPosFromBottom=((phHeight-yPixlesFromBottom)-(crSize.Height/2));
//通过计算图像宽度的中心来确定其x坐标
浮点数=FIMG=(phWidth/2);
//通过将文字对齐设置为居中来定义文字布局
StringFormat StrFormat=新StringFormat();
StrFormat.Alignment=StringAlignment.Center;
//定义一个半透射黑色的笔刷(Alpha设置为153)
SolidBrush-semiTransBrush2=新的SolidBrush(颜色为argb(153,0,0,0));
//绘制版权字符串
//grPhoto.DrawString(版权所有,//文本字符串
//crFont,//font
//半反式刷子2,//刷子
//新的点f(xCenterOfImg+5,yPosFromBottom+5),//位置
//标准格式);
//定义半透光白色的笔刷(Alpha设置为153)
SolidBrush semiTransBrush=新的SolidBrush(Color.FromArgb(153255 255 255));
//再次绘制版权字符串以创建阴影效果
//确保将此文本向右移动1个像素,向下移动1个像素
graphoto.DrawString(“AlexMaslakov.ru”,//文本字符串
crFont,//font
半转刷,//刷
新的点f(xCenterOfImg,yPosFromBottom),//位置
StrFormat);//文本对齐
//------------------------------------------------------------
//步骤2-插入水印图像
//------------------------------------------------------------
//基于先前修改的照片位图创建位图
位图bmWatermark=新位图(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
//将此位图加载到新的图形对象中
Graphics grWatermark=Graphics.FromImage(bmWatermark);
//为了实现易逝水印,我们将应用(2)颜色
//通过定义ImageAttributes对象和
//设置(2)其属性。
图像属性
// Open the file here
Image original = Image.FromFile(file);
// Create a MemoryStream in which to save the image
MemoryStream originalStream = new MemoryStream();
// Save the Image to the MemoryStream
original.Save(originalStream, original.RawFormat);
// Dispose the original image, so you can overwrite it later
original.Dispose();
//Replace the original photgraphs bitmap with the new Bitmap
imgPhoto.Dispose();
imgPhoto = bmWatermark;