我正在弄一个上传并生成水印的方法,找到一个比较简洁可用的,但有一个问题,就是源图片和最后生成带有水印的图片不能是同一张,所以想考虑先复制原图,再用复制的原图生成水印,最后删掉原图!不知我这个思路正不正确,请高人指点这是我在网上找来生成水印的函数,感觉很不错,如果能改成我上面说的问题,就很完美了!
代码如下:
/// <summary>
/// 给图片加水印
/// </summary>
/// <param name="strPath">原始文件</param>
/// <param name="strPathSyp">水印文件</param>
/// <param name="strPathSypf">加水印后的结果文件</param>
/// public static void AddWaterPic(string strPath, string strPathSyp, string strPathSypf)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(strPath);
Bitmap bm = new Bitmap(image); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(strPathSyp); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); //如果要使水印居中显示,只要更改bm.Width - copyImage.Width, bm.Height - copyImage.Height,这两个值 //就可以了,在实际项目中,我用的是Convert.ToInt32(image.Width * 0.3), Convert.ToInt32(image.Height * 0.3) g.DrawImage(copyImage, new System.Drawing.Rectangle(bm.Width - copyImage.Width, bm.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
copyImage.Dispose();
bm.Save(strPathSypf);
bm.Dispose();
}
代码如下:
/// <summary>
/// 给图片加水印
/// </summary>
/// <param name="strPath">原始文件</param>
/// <param name="strPathSyp">水印文件</param>
/// <param name="strPathSypf">加水印后的结果文件</param>
/// public static void AddWaterPic(string strPath, string strPathSyp, string strPathSypf)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(strPath);
Bitmap bm = new Bitmap(image); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(strPathSyp); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); //如果要使水印居中显示,只要更改bm.Width - copyImage.Width, bm.Height - copyImage.Height,这两个值 //就可以了,在实际项目中,我用的是Convert.ToInt32(image.Width * 0.3), Convert.ToInt32(image.Height * 0.3) g.DrawImage(copyImage, new System.Drawing.Rectangle(bm.Width - copyImage.Width, bm.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
copyImage.Dispose();
bm.Save(strPathSypf);
bm.Dispose();
}
private void importPhoto()
{
string strPhotoPath= Server.MapPath("../UploadFile/");
if (string.IsNullOrEmpty(File1.PostedFile.FileName))
{
string strPath = File1.PostedFile.FileName;
try
{
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
strPath = strPath.Substring(strPath.LastIndexOf("\\") + 1);
File1.PostedFile.SaveAs(strPhotoPath + strPath);
PicMark wm = new PicMark();
wm.PMark(new PicMark(), Server.MapPath("../ffy_images/") + ".png", strPhotoPath + strPath, strPhotoPath + fileName + extension);
strPath = fileName + extension;
}
catch ()
{
}
} }
public class pic
{
private string modifyImagePath = null;
private string drawedImagePath = null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent = 70;
private string outPath = null;
public pic()
{
}
public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(
drawedImage,
new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
0, 0, drawedImage.Width, drawedImage.Height,
GraphicsUnit.Pixel, imgAttr);
string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo file = new FileInfo(this.ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (file.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
if (this.OutPath == null || this.OutPath == "")
{
File.Delete(this.ModifyImagePath);
fs = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
}
else
{
fs = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
}
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
}