你会画图吗?

     在Asp.net中学习输出图片,本质就是“画图”,关键是你会“画图”吗?
     画图可以有好多种,包括输出水印图片——画加了logo的图片;输出验证码图片——画加了字符串的图片;输出缩略图片——将原图片画小了;输出防盗链图片——这个嘛好像不用“画”。
     第一步:我们简单地来回顾如何输出自己完全“画”的图片——写字符串并设置边框颜色
//第一步:准备“画板”
//Bitmap类继承了抽象类——Image类
using (Bitmap bitmap = new Bitmap(200, 100))
{
//第二步:准备“画笔”
Graphics g = Graphics.FromImage(bitmap);
//第三步:开始“画画”
//在“画板“上写好文字

铅笔 文字 字体 大小 笔的颜色 从哪个坐标位置开始
//g.DrawString("IT人民", new Font("楷体", 24), Brushes.Red, 10, 10);

填充白色背景
刷子 
//g.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
//g.DrawString("IT人民", new Font("楷体", 24), Brushes.Red, 10, 10);

//填充白色背景,留红边
g.FillRectangle(Brushes.Red, 0, 0, bitmap.Width, bitmap.Height);
g.FillRectangle(Brushes.White, 1, 1, bitmap.Width - 2, bitmap.Height - 2);
g.DrawString("IT人民", new Font("楷体", 24), Brushes.Red, 10, 10);
//第四步:把内存中的图片输出到浏览器
// 把文件保存到响应的输出流 输出文件格式
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
     第二步:输出加上字符串内容的图片——在图片中加上字符串
//获得绝对路径
string path = context.Request.MapPath("01.jpg");
//“画板”
using (Image image = Image.FromFile(path))
{
//“画笔”
Graphics g = Graphics.FromImage(image);
//在“画板“上写好文字
g.DrawString("司机41号", new Font("楷体", 41), Brushes.Blue, 10, 10);
//把内存中的图片输出到浏览器
image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
     第三步:输出水印图片——将第二步中的字符串替换成图片,就是输出水印图片了:
//原始图片路径
string path = context.Request.MapPath("01.jpg");
//水印图片路径
string logoPath = context.Request.MapPath("logo.jpg");
//原始图片,"画板"
using (Image img = Image.FromFile(path))
{
//水印图片,“画笔”
using (Image logoImg = Image.FromFile(logoPath))
{
//把水印图片画到原始图片上
Graphics g = Graphics.FromImage(img);
//画到哪个位置
int x = img.Width - logoImg.Width;
int y = img.Height - logoImg.Height;
g.DrawImage(logoImg, x, y, logoImg.Width, logoImg.Height);
//输出到浏览器
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
     第四步:输出验证码图片——就是将需要输出在图片上的字符串事先准备好,然后画在背景图片上就可以了:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";

string code = CreateCode();
using (Image img = CreateImg(code))
{
img.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

//生成验证码图片
Image CreateImg(string code)
{
Bitmap bitmap = new Bitmap(80, 40);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.FillRectangle(Brushes.Red, 0, 0, bitmap.Width, bitmap.Height);
g.FillRectangle(Brushes.White, 1, 1, bitmap.Width - 2, bitmap.Height - 2);
g.DrawString(code, new Font("楷体", 24), Brushes.Red, 5, 5);
return bitmap;
}
}

//生成随机数
string CreateCode()
{
string str = "0123456789abcdefg";
string s = "";
Random r = new Random();
for (int i = 0; i < 4; i++)
{
s += str[r.Next(0, str.Length)].ToString();
}
return s;
}
     第五步:输出缩略图片——本质就是将原来的图片按一定的比例画到规定大小的边框中:
string path = context.Request.MapPath("images/" + s);
//原始图片
using (Image img = Image.FromFile(path))
{
int width = 150;
int height = 100;
//生成缩略图,保证高宽比例
if (img.Width > img.Height)
{
height = width * img.Height / img.Width;
}
else
{
width = height * img.Width / img.Height;
}
//在内存创建空的图片,缩略图
using (Bitmap bitmap = new Bitmap(150, 100))
{
//此时“画板”是缩略图
using (Graphics g = Graphics.FromImage(bitmap))
{
//将原始图片画到缩略图中
g.DrawImage(img, 0, 0, width, height);
//输出缩略图
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
     第六步:输出防盗链图片
     输出防盗链图片不需要“画图”,而是为了保护图片不被其它网站“利用”,网站设计的一种安全模式。
     输出防盗链图片,在这里需要用到一个全局处理程序的知识点,步骤也非常简单,两步走:
     第1步:建一个类WaterMaker.cs,默认会创建文件夹App_Code并将该类放到文件夹下。
     WaterMaker.cs类必须继承IHttpHandler接口。
     第2步:设置,在配置文件web.config中,找到<system.web>—》<httpHandlers>节点
     <system.web>
          <httpHandlers>
          <!---请求方式get,post或者全部 哪一组请求  交给WaterMaker类处理-->
          <!--<add verb="*" path="images/*.jpg" type="WaterMaker"/>-->
          //当直接请求images文件夹下的图片,就会加上水印。

     附上代码画图

     备注:写于2013年10月3日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值