asp.net生成缩略图、文字图片水印
/// <summary>
/// 会产生graphics异常的PixelFormat
/// </summary>
private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
PixelFormat.Format8bppIndexed
}; /// <summary>
/// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
/// </summary>
/// <param name="imgPixelFormat">原图片的PixelFormat</param>
/// <returns></returns>
private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
{
foreach (PixelFormat pf in indexedPixelFormats)
{
if (pf.Equals(imgPixelFormat)) return true;
} return false;
} /// <summary>
/// 在图片上生成图片水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_syp">生成的带图片水印的图片路径</param>
/// <param name="Path_sypf">水印图片路径</param>
public static void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), , , copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose(); image.Save(Path_syp);
image.Dispose();
}
/// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
public static void AddShuiYinWord(string Path, string Path_sy)
{ using (System.Drawing.Image img = System.Drawing.Image.FromFile(Path))
{
//如果原图片是索引像素格式之列的,则需要转换
if (IsPixelFormatIndexed(img.PixelFormat))
{
Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(img, , ); System.Drawing.Font f = new System.Drawing.Font("Verdana", );
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
g.DrawString("爱智旮旯", f, b, , ); }
bmp.Save(Path_sy);
bmp.Dispose(); }
else //否则直接操作
{
string addText = "爱智旮旯";
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(image, , , image.Width, image.Height);
System.Drawing.Font f = new System.Drawing.Font("Verdana", );
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue); g.DrawString(addText, f, b, , );
g.Dispose(); image.Save(Path_sy);
image.Dispose();
}
} } /// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width;
int toheight = height; int x = ;
int y = ;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = ;
x = (originalImage.Width - ow) / ;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = ;
y = (originalImage.Height - oh) / ;
}
break;
default:
break;
} //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(, , towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
} protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/jpg")
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径 FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
string fileName_s = "s_" + file.Name; // 缩略图文件名称
string fileName_sy = "sy_" + file.Name; // 水印图文件名称(文字)
string fileName_syp = "syp_" + file.Name; // 水印图文件名称(图片)
//以下路径可以根据情况进行修改
string webFilePath = Server.MapPath("file/" + fileName); // 服务器端文件路径
string webFilePath_s = Server.MapPath("file/" + fileName_s); // 服务器端缩略图路径
string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // 服务器端带水印图路径(文字)
string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // 服务器端带水印图路径(图片)
//以下为水印图片的路径一定要先准备一张水印图片!
string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // 服务器端水印图路径(图片) if (!File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
AddShuiYinWord(webFilePath, webFilePath_sy);
//AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
MakeThumbnail(webFilePath, webFilePath_s, , , "Cut"); // 生成缩略图方法
Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
}
catch (Exception ex)
{
Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
}
}
else
{
Label1.Text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
Label1.Text = "提示:文件类型不符";
} }
}
asp.net生成缩略图、文字图片水印的更多相关文章
- C#上传图片和生成缩略图以及图片预览
因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...
- php 使用GD库压缩图片,添加文字图片水印
先上一个工具类,提供了压缩,添加文字.图片水印等方法: image.class.php <?php class Image { private $info; private $image; pu ...
- PHP生成缩略图、加水印
<?php class ThumbWaterImages{ /** * 生成缩略图/加水印 * classname ThumbWaterImages * datetime:2015-1-15 * ...
- asp.net生成缩略图
/// <summary> /// 生成缩略图 /// </summary> /// <param name="orginalImagePat"> ...
- java生成竖排文字图片
package com.kadang.designer.web.action;import java.awt.Color;import java.awt.Font;import java.awt.Fo ...
- ASP.NET生成缩略图的代码
01. // <summary> 02. /// 生成缩略图 03. /// </summary> 04. /// &l ...
- C# 生成缩略图 去除图片旋转角度
图片生成缩略图会有旋转角度 /// <summary> /// 测试JRE图片压缩后图片会旋转问题 /// </summary> public void Uploadimg1( ...
- PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转
[强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...
- ASP.NET 生成缩略图片类分享
/// <summary> /// 生成图片缩略图 指定文件路径生成 /// </summary> public static void SaveImage(String fu ...
随机推荐
- 【HeadFirst 设计模式总结】1.策略模式
1.书中举了一个鸭子类的设计,有些会飞或者会叫,有些不会飞可能也不会叫,用继承则导致不该有的功能通过继承而继承了下来,使用接口则代码无法做到最大程度的重用.进而引出设计原则1:找出应用中可能需要变化之 ...
- css阴影--box-shadow的用法
原文:http://blog.csdn.net/freshlover/article/details/7610269 text-shadow是给文本添加阴影效果,box-shadow是给元素块添加周边 ...
- OSError: [Errno 13] Permission denied: '/etc/cron.d/1sandbox_registration'
使用Hortonworks 的twitter tutorial: http://hortonworks.com/hadoop-tutorial/how-to-refine-and-visualize- ...
- MVC布局页占位符@RenderSection("bscript", false)
@RenderSection("bscript", false) //false表示不是必须填充 填充bscript占位符 @section bscript{}
- (导航控制器view)全屏幕滑动实现pop效果
看到现在app 中越来越流行的手势滑动实现pop效果,心里很是痒痒跃跃欲试,经过多方查看资料,终于在简书上找到了详细的实现方案: 轻松学习之二——iOS利用Runtime自定义控制器POP手势动画 经 ...
- Method to fix "Naming information cannot be located because the target principal name is incorrect." for AD replication failure
Assume Failure DC FP01 and Working DC DC01 1. Stop the Key Distribution Center (KDC) service on FP01 ...
- css基础之 联网使用bootstrap
在<head></head>中添加 <meta charset="utf-8"> <meta http-equiv="X-UA- ...
- C学习-fgets()篇1
学习fgets()函数时发现了一个问题,先贴代码 #include<stdio.h> #include<string.h> #include<ctype.h> vo ...
- [转]activiti5用户任务分配
用户任务分配办理人:1.用户任务可以直接分配给一个用户,这可以通过humanPerformer元素定义. humanPerformer定义需要一个 resourceAssignmentExpressi ...
- ORACLE CHECK CONSTRAINT使用示例(转载) .
看下面的例子: CREATE TABLE temp (age NUMBER(3)); ALTER TABLE temp ADD CONSTRAINT ck_temp_age CHECK ((A ...