c# asp.net 给一个图片打上水印,并且上传
2009-08-25 14:08
351 查看
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload
{
private string BasePath;
public FileUpload()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 给图片上水印
/// </summary>
/// <param name="filePath">原图片地址</param>
/// <param name="waterFile">水印图片地址</param>
public void MarkWater(string filePath, string waterFile)
{
//GIF不水印
int i = filePath.LastIndexOf(".");
string ex = filePath.Substring(i, filePath.Length - i);
if (string.Compare(ex, ".gif", true) == 0)
{
return;
}
string ModifyImagePath = BasePath + filePath;//修改的图像路径
int lucencyPercent = 25;
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
//建立图形对象
modifyImage = Image.FromFile(ModifyImagePath, true);
drawedImage = Image.FromFile(BasePath + waterFile, true);
g = Graphics.FromImage(modifyImage);
//获取要绘制图形坐标
int x = modifyImage.Width - drawedImage.Width;
int y = modifyImage.Height - drawedImage.Height;
//设置颜色矩阵
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)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), 10, 10, drawedImage.Width, drawedImage.Height, GraphicsUnit.Pixel, imgAttr);
//保存文件
string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo fi = new FileInfo(ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (fi.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;
File.Delete(ModifyImagePath);
fs = new FileStream(ModifyImagePath, FileMode.Create, FileAccess.Write);
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload
{
private string BasePath;
public FileUpload()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 给图片上水印
/// </summary>
/// <param name="filePath">原图片地址</param>
/// <param name="waterFile">水印图片地址</param>
public void MarkWater(string filePath, string waterFile)
{
//GIF不水印
int i = filePath.LastIndexOf(".");
string ex = filePath.Substring(i, filePath.Length - i);
if (string.Compare(ex, ".gif", true) == 0)
{
return;
}
string ModifyImagePath = BasePath + filePath;//修改的图像路径
int lucencyPercent = 25;
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
//建立图形对象
modifyImage = Image.FromFile(ModifyImagePath, true);
drawedImage = Image.FromFile(BasePath + waterFile, true);
g = Graphics.FromImage(modifyImage);
//获取要绘制图形坐标
int x = modifyImage.Width - drawedImage.Width;
int y = modifyImage.Height - drawedImage.Height;
//设置颜色矩阵
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)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), 10, 10, drawedImage.Width, drawedImage.Height, GraphicsUnit.Pixel, imgAttr);
//保存文件
string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo fi = new FileInfo(ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (fi.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;
File.Delete(ModifyImagePath);
fs = new FileStream(ModifyImagePath, FileMode.Create, FileAccess.Write);
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
}
相关文章推荐
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- ASP.net 为上传的图片加上一个logo图片水印(生成图片质量非常高)
- ASP.NET\MVC 解决C#上传图片质量下降,图片模糊,水印有杂点的问题
- [网络收集]使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- C# asp.net上传图片加水印文字(个人心得)
- 用asp.net 多文件上传 (并且图片即使显示)并隐藏上一个文件选择器
- ASP.NET(C#)上传图片加上文字或图片水印
- 将上传图片打上防伪图片水印并写入数据库ASP.NET C#
- C# asp.net上传图片加水印文字(个人心得)
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- asp.net上传图片加水印(c#)
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- Asp.net上传图片同时生成缩略图和水印图
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- asp.net上传生成略铺图,文字水印图,图片水印图